Expand description
Per-vendor on-disk cache with atomic writes, TTL checks, and inter-process locking.
Mirrors claudebar’s cache layout but per-vendor:
~/.cache/ai-usagebar/<vendor>/usage.json payload
~/.cache/ai-usagebar/<vendor>/.stale marker (cache is stale)
~/.cache/ai-usagebar/<vendor>/.last_error HTTP code\nmessage
~/.cache/ai-usagebar/<vendor>/.fetch.lock flock target
Multi-monitor safety: callers should acquire_lock() before the refresh+
fetch window, mirroring claudebar:402-407’s exec 9>"$_lockfile" / flock.
Structs§
- Cache
- Per-vendor cache directory and helper API.
- Lock
Guard - Releases the flock on drop. Holding this across an
.awaitis fine as long as you don’t move it across tasks (we always use it intokio::mainon a single thread).
Constants§
- DEFAULT_
TTL - Default TTL — claudebar’s
CACHE_TTL=60. - MAX_
STALE - Maximum staleness before we refuse to serve cached data even on failure.
Mirrors claudebar’s
WEEKLY_WINDOW(7 days).
Functions§
- acquire_
lock - Acquire an exclusive flock on
path, blocking up totimeout. Returned guard releases the lock on drop. - atomic_
write - Atomic write helper used by
write_last_error. Public for vendors that need to write small sidecar files (credentials, etc.). - home_
dir - The user’s home directory, resolved cross-platform via
directories($HOMEon Unix/macOS,%USERPROFILE%/ the Known Folder on Windows).