@@ -3,26 +3,47 @@ local M = {}
33
44local config = require (" ccusage.config" )
55
6- -- Simple cache
6+ -- Simplified cache with 5-second TTL
77local cache = {
8- data = nil ,
9- last_update = 0 ,
10- availability = nil ,
8+ blocks = { data = nil , timestamp = 0 },
9+ availability = { data = nil , timestamp = 0 },
1110}
1211
13- --- Simple jobstart to get ccusage blocks
14- --- @return CCUsage.Data ?
15- M .ccusage_blocks = function ()
16- -- Check cache first (5 second cache)
17- --- @diagnostic disable-next-line : undefined-field
18- local now = vim .uv .now ()
12+ local CACHE_TTL = 5000 -- 5 seconds in milliseconds
1913
20- if cache .data and (now - cache .last_update ) < 5000 then
21- return cache .data
14+ --- Check if cached value is still valid
15+ --- @param cache_entry table cache entry with data and timestamp
16+ --- @return boolean
17+ local function is_cache_valid (cache_entry )
18+ if not cache_entry .data then
19+ return false
2220 end
21+ --- @diagnostic disable-next-line : undefined-field
22+ return (vim .uv .now () - cache_entry .timestamp ) < CACHE_TTL
23+ end
2324
24- -- Update timestamp immediately to prevent multiple jobs
25- cache .last_update = now
25+ --- Update cache entry
26+ --- @param cache_entry table cache entry to update
27+ --- @param data any data to cache
28+ local function update_cache (cache_entry , data )
29+ cache_entry .data = data
30+ --- @diagnostic disable-next-line : undefined-field
31+ cache_entry .timestamp = vim .uv .now ()
32+ end
33+
34+ --- Get ccusage blocks data with simple caching
35+ --- @param opts ? { bypass_cache ?: boolean , callback ?: fun ( data : CCUsage.Data ?)}
36+ --- @return CCUsage.Data ?
37+ M .ccusage_blocks = function (opts )
38+ opts = opts or {}
39+
40+ -- Return cached data if valid and not bypassing cache
41+ if not opts .bypass_cache and is_cache_valid (cache .blocks ) then
42+ if opts .callback then
43+ opts .callback (cache .blocks .data )
44+ end
45+ return cache .blocks .data
46+ end
2647
2748 local base_cmd = config .options .ccusage_cmd
2849
@@ -33,20 +54,39 @@ M.ccusage_blocks = function()
3354 local result = table.concat (data , " \n " )
3455 local ok , parsed = pcall (vim .json .decode , result )
3556 if ok then
36- cache .data = parsed
57+ -- Update cache only if not bypassing
58+ if not opts .bypass_cache then
59+ update_cache (cache .blocks , parsed )
60+ end
61+ if opts .callback then
62+ opts .callback (parsed )
63+ end
3764 end
3865 end
3966 end ,
67+ on_exit = function (_ , exit_code , _ )
68+ if exit_code ~= 0 and opts .callback then
69+ opts .callback (nil )
70+ end
71+ end ,
4072 })
4173
42- return cache .data
74+ -- Return cached data if available and not bypassing cache
75+ return not opts .bypass_cache and cache .blocks .data or nil
4376end
4477
45- --- Check if ccusage CLI is available
78+ --- Check if ccusage CLI is available with simple caching
79+ --- @param opts ? { bypass_cache ?: boolean , callback ?: fun ( available : boolean )}
4680--- @return boolean
47- M .is_available = function ()
48- if cache .availability ~= nil then
49- return cache .availability
81+ M .is_available = function (opts )
82+ opts = opts or {}
83+
84+ -- Return cached result if valid and not bypassing cache
85+ if not opts .bypass_cache and is_cache_valid (cache .availability ) then
86+ if opts .callback then
87+ opts .callback (cache .availability .data )
88+ end
89+ return cache .availability .data
5090 end
5191
5292 local base_cmd = config .options .ccusage_cmd
@@ -59,19 +99,27 @@ M.is_available = function()
5999 end
60100 end ,
61101 on_exit = function (_ , exit_code , _ )
62- cache .availability = (exit_code == 0 and found )
102+ local available = (exit_code == 0 and found )
103+ -- Update cache only if not bypassing
104+ if not opts .bypass_cache then
105+ update_cache (cache .availability , available )
106+ end
107+ if opts .callback then
108+ opts .callback (available )
109+ end
63110 end ,
64111 })
65112
66- return cache .availability or false
113+ -- Return cached value if available and not bypassing cache
114+ return not opts .bypass_cache and (cache .availability .data or false ) or false
67115end
68116
69- --- Force refresh
117+ --- Force refresh blocks data
70118--- @return CCUsage.Data ?
71119M .refresh_blocks = function ()
72- cache .data = nil
73- cache .last_update = 0
74- return M .ccusage_blocks ()
120+ cache .blocks . data = nil
121+ cache .blocks . timestamp = 0
122+ return M .ccusage_blocks ({ bypass_cache = true } )
75123end
76124
77125return M
0 commit comments