@argos-js/cache
v0.1.2
Published
Modular, runtime-agnostic caching suite for Argos-JS with Pulse CDC Near-Cache, Redis, ArgosDB Table and In-Memory drivers
Readme
@argos-js/cache
High-performance, modular, and runtime-agnostic caching suite for Argos-JS, featuring sub-millisecond In-Memory RAM reads (< 0.05ms) and instant reactive cache invalidation driven by ArgosDB Pulse CDC (argosdb-pulse).
Key Features
- Runtime-Agnostic & Zero Addons: 100% pure TypeScript using standard Web APIs (
Map,Uint8Array,TextEncoder,TextDecoder,crypto). Runs seamlessly across Node.js 18+, Bun, and Deno. - Pulse Near-Cache Driver (
PulseNearCacheDriver):- L1 In-Memory RAM: Serves hot cache entries directly from process heap in microseconds without network hops.
- Reactive CDC Invalidation: Subscribes to ArgosDB Pulse streams (
cdc.<table_name>.*). When anUPDATEorDELETEoccurs in the database, stale keys are purged from L1 cache in the exact same event cycle, completely solving the stale cache problem.
- Pluggable Drivers:
pulse: Near-Cache combining L1 RAM with real-time CDC invalidation (default with ArgosDB).memory: In-memory driver with configurable LRU eviction and TTL expiration.redis: Pluggable Redis adapter with prefix and tag support (optional dependency, no runtime crash if unused).argosdb: Distributed table-backed cache in ArgosDB (app_cache), sharing cache across cluster workers without deploying separate Redis infrastructure.
- Batch Tag Invalidation: Group related cache keys by tags (e.g.
['users', 'tenant_123']) and purge them in a single call. - Fetch-Through (
getOrSet): Transparent read-through pattern with automatic population and TTL configuration.
Installation
pnpm add @argos-js/cacheConfiguration
Configure the active driver via code or environment variables:
# Driver selection: pulse | memory | redis | argosdb
CACHE_DRIVER=pulseInitializing CacheManager
import { createCache } from '@argos-js/cache';
export const cache = createCache({
driver: (process.env.CACHE_DRIVER as any) || 'pulse',
prefix: 'argos_api:',
defaultTtl: 300, // 5 minutes default TTL
pulse: {
listenCdc: true,
watchedTables: ['auth_users', 'products'],
},
});Usage
1. Fetch-Through (getOrSet)
const user = await cache.getOrSet(`user:${userId}`, async () => {
return await db.select().from(users).where(eq(users.id, userId)).first();
}, { ttl: 600 });2. Manual Set & Get
// Store item with 60s TTL and tags
await cache.set('product:123', productData, {
ttl: 60,
tags: ['products', 'category_electronics'],
});
const product = await cache.get('product:123');3. Invalidation by Tag
// Purges all products when catalog changes
await cache.invalidateByTag('products');Near-Cache CDC Architecture
┌────────────────────────────────────────────────────────┐
│ Application │
└──────────────────────────┬─────────────────────────────┘
│
┌───────────────┴───────────────┐
▼ ▼
[1] Fast Cache Read [2] Cache Miss
L1 RAM (< 0.05ms) Database Query
│ │
│ ArgosDB Mutation
│ │
│ ▼
│ ArgosDB Pulse (WAL/CDC)
│ │
│ TCP Binary Stream (Port 8788)
│ │
│ ▼
└──────── [3] Invalidate L1 ────┘
(Instant Purge on UPDATE/DELETE)License
MIT © Argos-JS Team
