@realvendex/pi-cache
v1.0.0
Published
Pi-native runtime cache and memoization toolkit for pi.dev extension tools: deterministic cache keys, TTL/SWR policies, tag/namespace invalidation, optional local persistence.
Maintainers
Readme
@realvendex/pi-cache
Pi-native runtime cache and memoization toolkit for pi.dev extension tools.
Installation
pi install npm:@realvendex/pi-cacheWhat It Does
pi-cache provides deterministic cache keys, TTL/stale-while-revalidate policies, tag-based invalidation, namespace isolation, and optional JSON file persistence for pi.dev extension tools. It's designed for caching expensive tool/provider results without any LLM or network calls.
Key features:
- Deterministic cache key generation from namespace + input hash (SHA-256)
- TTL with stale-while-revalidate (SWR) support
- Tag-based and namespace-based group invalidation
- Glob pattern matching for key invalidation
- Expired-TTL sweep mode
- Optional LRU eviction with configurable max entries and size limits
- Optional JSON file persistence (atomic writes)
- Fully deterministic — no LLM calls, no network calls
Tools
cache_get
Look up a cached value by namespace + key or input hash.
Parameters:
namespace(string, default:"default") — Cache namespace for isolationkey(string, optional) — Explicit cache keyinput(object, optional) — Object to hash for deterministic key generation (mutually exclusive with key)allowStale(boolean, default:false) — Return stale entries within SWR windownow(number, optional) — Override current time in ms (for testing)persistencePath(string, optional) — Load cache from JSON file before lookup
Example:
Use the cache_get tool with namespace="api", input={"userId": "123"}, allowStale=truecache_set
Store a JSON-serializable value with TTL, tags, and namespace isolation.
Parameters:
namespace(string, default:"default") — Cache namespacekey(string, optional) — Explicit cache keyinput(object, optional) — Object to hash for deterministic keyvalue(any, required) — JSON-serializable value to cachettlMs(number, default:60000) — Time-to-live in millisecondsstaleWhileRevalidateMs(number, default:0) — SWR window after TTL expirestags(string[], default:[]) — Tags for group invalidationmaxEntries(number, optional) — Max entries in namespace (LRU eviction)maxSizeBytes(number, optional) — Max approximate size in bytespersistencePath(string, optional) — Persist cache to JSON file after write
Example:
Use the cache_set tool with namespace="api", input={"userId": "123"}, value={...}, ttlMs=30000, tags=["user", "profile"]cache_invalidate
Remove cache entries by key, namespace, tag, glob pattern, or expired-TTL sweep.
Parameters:
key(string, optional) — Exact cache key to invalidatenamespace(string, optional) — Invalidate all entries in namespacetag(string, optional) — Invalidate all entries with this tagpattern(string, optional) — Glob pattern to match keys (e.g."user:*")expiredOnly(boolean, default:false) — Only invalidate expired entries (sweep mode)now(number, optional) — Override current time in ms (for testing)persistencePath(string, optional) — Persist updated cache to file
Example:
Use the cache_invalidate tool with tag="user", persistencePath="./cache.json"Persistence
When persistencePath is provided to any tool, the cache is loaded from / saved to a JSON file. Writes are atomic (temp file + rename). Corrupt or missing files are tolerated gracefully.
// Cache persists across tool calls when using the same path
cache_set → persistencePath="./cache.json" // writes to disk
cache_get → persistencePath="./cache.json" // reads from diskTTL & Stale-While-Revalidate
- TTL: Entries expire after
ttlMsmilliseconds - SWR: After TTL expires, entries remain accessible for
staleWhileRevalidateMsifallowStale=true - Sweep: Use
cache_invalidatewithexpiredOnly=trueto clean up fully expired entries
Integrations
- pi-rate-limit: Cache before rate-limited provider/tool calls
- pi-log: Emit cache hit/miss/eviction events in consuming apps
- pi-config: Load cache policies (TTL, max entries, persistence path)
- pi-perf: Measure hit-rate and latency savings
- pi-token-router: Cache provider responses where safe
- pi-ci: Validate cache tests in generated workflows
Resources
License
MIT
