sonamu.config.ts.
What is BentoCache?
BentoCache is a TypeScript cache library that supports multi-tier caching. Key Features:- L1/L2 Layers: Memory (fast) + Persistent storage (slower but shareable)
- Various Drivers: Memory, Redis, File, Knex support
- Bus System: Distributed cache invalidation
- Tag-based Invalidation: Manage multiple caches as groups
- TTL & Grace Period: Expiration and Stale-While-Revalidate
Basic Configuration
sonamu.config.ts
Add cache configuration to theserver.cache field in sonamu.config.ts:
default: Name of the default store to usestores: Store configuration object (at least 1 required)
Driver Types
Sonamu provides 5 types of drivers:memory
Memory-based cache
(Fast but cleared on process restart)
redis
Redis-based cache
(Shareable across multiple processes)
file
File system-based cache
(Persistent storage)
knex
Database-based cache
(Uses existing DB)
redisBus
Distributed cache invalidation bus
(Synchronization across multiple servers)
Import Methods
Store Configuration
L1 Layer (Memory Cache)
L1 is stored in local memory and is the fastest.- Valid only within the process
- Cleared on server restart
- No network I/O (fastest)
L2 Layer (Persistent Storage)
L2 is shareable storage across multiple processes/servers.- Redis
- File
- Knex (DB)
- Share cache across multiple servers
- Persistent storage (retained after restart)
- Fast network access
Bus Layer (Distributed Invalidation)
When you have multiple servers, deleting cache on one server notifies the others.Multi-Store Configuration
You can use multiple stores for different purposes:Using Stores
Practical Examples
1. Single Server (Memory Only)
- Single server operation
- Fast cache regeneration
- Infrequent server restarts
2. Multiple Servers (Redis Shared)
- Load balancer + multiple servers
- Need to share cache across servers
- Maintain cache after server restart
3. Tier-Based Configuration (Performance Optimization)
Driver Options in Detail
Memory Driver
"10kb", "5mb", "1gb" or bytes (1024)