Basic Structure
default
Specifies the default cache store to use. Type:string
stores
Defines the cache stores to use. Type:Record<string, BentoCacheStore>
Single Layer: Memory
The simplest configuration, storing cache only in memory.Cons: Cache lost on server restart, cannot share between multiple servers
Multi-layer Cache: L1 (Memory) + L2 (Redis)
L1 caches quickly in memory, L2 caches persistently in Redis.- Check L1 (memory) first when querying cache
- If not in L1, check L2 (Redis)
- If found in L2, also store in L1
- If not in both, query original data and store in L1, L2
Distributed Cache: L1 + L2 + Bus
Synchronizes cache invalidation across multiple servers.- When cache is invalidated on one server
- Propagated to all other servers via Bus
- L1 cache on all servers is also automatically invalidated
ttl
Sets the default Time To Live for cache. Type:string (optional)
Default: "5m" (5 minutes)
"5s"- 5 seconds"1m"- 1 minute"1h"- 1 hour"1d"- 1 day
prefix
Sets a prefix to be added to all cache keys. Type:string (optional)
Default: ""
Driver Options
memory driver
Stores cache in memory.maxSize: Maximum memory usage ("50mb","100mb","1gb", etc.)
redis driver
Stores cache in Redis.redisBus driver
Propagates cache invalidation using Redis Pub/Sub.redisBus can use the same connection as the redis driver.Practical Examples
Single Server: Memory Only
Multi-layer Cache: Memory + Redis
Distributed Environment: Memory + Redis + Bus
Multiple Stores: Separated by Purpose
Using Cache
After configuration, use the@cache decorator in APIs.
Installing Redis
Redis must be installed before using it.Docker
macOS
Linux
Connection Verification
Important Notes
1. Redis Connection Management
2. Memory Size Configuration
3. TTL Selection
Next Steps
After completing cache configuration:- cache decorator - Apply caching to APIs
- cache-control - HTTP cache control
- cache strategies - Advanced caching strategies