@ooneex/cache component is a multi-backend caching layer. Every backend implements the same ICache interface — get, set, delete, deleteByPrefix, has, clear — so you can start on the filesystem and move to Redis without changing a single call site. Values are JSON-serialized automatically and support TTL expiration.
Why this component
- One interface, many backends. Filesystem and Redis both implement
ICache— swap backends without touching callers. - Type-safe values.
get<T>()andset<T>()carry your value type through serialization. - TTL built in. Set a per-key time-to-live in seconds; expired entries return
undefined. - Bulk operations.
deleteByPrefix()clears related keys in one call;clear()empties the cache. - Container-managed. Register a cache class with a decorator and resolve it from the container.
How it works
You pick (or implement) a backend and register it. Reads and writes go through theICache methods; the backend handles serialization, namespacing, and expiration.
| Method | Purpose |
|---|---|
get<T>(key) | Return the value or undefined if missing/expired. |
set<T>(key, value, ttl?) | Store a value, optionally expiring after ttl seconds. |
delete(key) | Remove one key; returns whether it existed. |
deleteByPrefix(prefix) | Remove every key starting with prefix; returns the count. |
has(key) | Whether a live (non-expired) key exists. |
clear() | Remove everything. |
| Backend | Best for | Distribution |
|---|---|---|
FilesystemCache | Local development, single-instance caches | One machine (disk) |
RedisCache | Production caches shared across instances | Shared (TCP) |
0 (or omitted) means the entry never expires. Redis backends namespace every key (default cache:) to avoid collisions on a shared instance.
Environment variables
| Variable | Backend | Required | Purpose |
|---|---|---|---|
CACHE_REDIS_URL | RedisCache | Yes (unless passed in config) | Connection string, e.g. redis://localhost:6379. Missing throws CacheException (URL_REQUIRED). |
FilesystemCache needs no environment variables; it defaults to a .cache directory in the working directory.
Usage
The API is the same regardless of backend.Decorator and usage
@decorator.cache()
Registers a cache class with the container. It accepts an optional scope (defaults to singleton). Use it to register a built-in backend under your own name, or a custom backend that implements ICache.
Exceptions
The component throwsCacheException when a backend is misconfigured or an operation fails. It carries a machine-readable key, a human-readable message, and a data object.
| Key | When |
|---|---|
URL_REQUIRED | RedisCache is created without a connection string or CACHE_REDIS_URL. |
MAX_SIZE_EXCEEDED | A FilesystemCache value exceeds the maximum file size (10MB by default). |
Best practices
- Namespace keys. Use a stable scheme like
user:123andsession:abcsodeleteByPrefix()can target related entries. - Always set a TTL for volatile data. Cache derived or remote data with an explicit
ttlso it can’t go stale indefinitely. - Invalidate on write. Delete (or overwrite) the key whenever the source changes; pair
delete()withdeleteByPrefix()for derived keys. - Pick the backend per environment. Filesystem for local/dev, Redis for shared production — the code stays identical.
- Keep values serializable. Stored values round-trip through JSON; avoid class instances, functions, and circular references.
- Treat the cache as optional. On a miss, fall back to the source of truth — never assume a key is present.
- Throw
CacheExceptionwith a stablekey. In custom backends, keep keys constant and put variable detail indata.
CLI command
Scaffold a cache adapter and its test file with the generator. It writes the class undermodules/<module>/src/cache/<Name>Cache.ts and installs @ooneex/cache if it is missing.
| Option | Description | Default |
|---|---|---|
--name | Cache class name. The Cache suffix is appended automatically. | Prompted if omitted |
--module | Target module the class is generated into. | shared |
--override | Overwrite an existing class without prompting. | false |
ICache stub, ready for you to implement each method against your backend:
Use with Claude and Codex
The generator ships a matchingcache:create skill. It runs the scaffold and then guides your AI agent through completing the adapter — implementing get, set, delete, and has against a real backend and wiring up its client. Initialize the skills once for your agent:
- Claude
- Codex
Prompt
cache:create --name=Session, then implements the ICache methods against a Redis client.