-
Notifications
You must be signed in to change notification settings - Fork 2
Cacher Engines
Built-in cache engine implementations for the Cacher package.
Runtime note:
MemoryCacheris process-local and works on every runtime, including Cloudflare Workers and the browser.RedisCacherandMemCacherneed a reachable TCP target — on Workers that's real TCP via@tundralibs/compat/net'scloudflare:socketsbackend, nonodejs_compatflag needed, but a plain browser has no raw TCP at all.
The @tundralibs/cacher/engines module re-exports all built-in cache engine classes and their option types. You can import individual engines directly rather than going through the Cacher manager.
| Engine | Identifier | External dependency | Description |
|---|---|---|---|
MemoryCacher |
'MEMORY' |
None | In-process memory cache |
RedisCacher |
'REDIS' |
Redis server | Redis-backed distributed cache |
MemCacher |
'MEMCACHED' |
Memcached server | Memcached-backed distributed cache |
WorkersKVCacher |
'WORKERS_KV' |
Workers KV binding | Cloudflare Workers KV cache |
Deno:
deno add @tundralibs/cacherBun:
bunx jsr add @tundralibs/cacherNode.js:
npx jsr add @tundralibs/cacherimport { Cacher } from '@tundralibs/cacher';
const cache = Cacher.create('MEMORY', 'my-cache', { defaultExpiry: 300 });
await cache.set('key', 'value');
const value = await cache.get<string>('key');import {
MemCacher,
MemoryCacher,
RedisCacher,
} from '@tundralibs/cacher/engines';
const memCache = new MemoryCacher('local', { defaultExpiry: 300 });
const redisCache = new RedisCacher('sessions', {
host: 'localhost',
port: 6379,
});
const memcachedCache = new MemCacher('objects', {
host: 'localhost',
port: 11211,
});Instance names may not contain
:. It is the reserved namespace separator (keys are stored as${name}:${key}), so allowing it would let one namespace become a colon-prefix of another and letclear()wipe a sibling. The rule is enforced on both creation paths, but the thrown error differs.Cacher.create(...)validates its arguments first and rejects the name with a baseCacherError(messageInstance name must not contain ":" ..., with nocodeproperty) before any engine is constructed. Constructing an engine directly (as above) instead reachesAbstractEngine's constructor check, which throws aCacherEngineError(CONFIG_INVALID).
All engines extend AbstractEngine and share this interface:
import { MemoryCacher } from '@tundralibs/cacher/engines';
const cache = new MemoryCacher('demo', {});
const data = { userId: 42 };
await cache.set('user:1', { name: 'Alice' });
await cache.set('session:x', data, { expiry: 600, window: true });| Parameter | Type | Default | Description |
|---|---|---|---|
key |
string |
— | Cache key |
value |
T |
— | JSON-serializable value |
options.expiry |
number |
defaultExpiry |
TTL in seconds (0 = no expiry) |
options.window |
boolean |
false |
Sliding expiry — resets TTL on each get
|
Returns the cached value, or undefined if missing or expired.
import { MemoryCacher } from '@tundralibs/cacher/engines';
type User = { name: string };
const cache = new MemoryCacher('demo', {});
const user = await cache.get<User>('user:1');Returns true if the key exists and has not expired.
import { MemoryCacher } from '@tundralibs/cacher/engines';
const cache = new MemoryCacher('demo', {});
if (await cache.has('feature-flag:beta')) { /* ... */ }Removes a single entry.
import { MemoryCacher } from '@tundralibs/cacher/engines';
const cache = new MemoryCacher('demo', {});
await cache.delete('user:1');Removes all entries in this instance's namespace. The mechanism is
backend-specific — Memory and Redis delete outright (Redis via KEYS +
DEL, not SCAN-based), Memcached and Workers KV switch to a new
version instead of deleting — see each engine's own doc for the tradeoffs:
Cacher-Redis.md#notes,
Cacher-Memcached.md#notes,
Cacher-WorkersKV.md#notes.
import { MemoryCacher } from '@tundralibs/cacher/engines';
const cache = new MemoryCacher('demo', {});
await cache.clear();Establishes the backend connection (Redis, Memcached). Called automatically by every operation — only call explicitly if you want to pre-connect.
import { MemoryCacher } from '@tundralibs/cacher/engines';
const cache = new MemoryCacher('demo', {});
await cache.init();Releases backend resources. Call during application shutdown.
import { MemoryCacher } from '@tundralibs/cacher/engines';
const cache = new MemoryCacher('demo', {});
await cache.finalize();| Capability | Memory | Redis | Memcached | Workers KV |
|---|---|---|---|---|
| No external dependencies | ✅ | ❌ | ❌ | ✅ |
| Shared across processes | ❌ | ✅ | ✅ | ✅ |
| TLS / SSL support | ❌ | ✅ | ✅ | n/a |
| Sliding (window) expiry | ✅ | ✅ | ✅ | ❌ |
| Per-entry custom TTL | ✅ | ✅ | ✅ | ✅* |
| Namespace isolation | ✅ | ✅ | ✅ | ✅ |
* Workers KV accepts 0 (no expiry) or at least 60 seconds.
- MemoryCacher — In-process cache, no dependencies
- RedisCacher — Redis-backed cache with TLS support
- MemCacher — Memcached-backed cache with TLS support
- WorkersKVCacher — Cloudflare Workers KV cache, Workers only