Skip to main content
Sonamu provides powerful caching capabilities based on BentoCache. This guide explains how to configure caching in 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 the server.cache field in sonamu.config.ts:
Required Fields:
  • default: Name of the default store to use
  • stores: 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.
Characteristics:
  • 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 Advantages:
  • 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.
Without Bus:
With Bus:

Multi-Store Configuration

You can use multiple stores for different purposes:

Using Stores

You can also specify stores in decorators:

Practical Examples

1. Single Server (Memory Only)

Suitable Cases:
  • Single server operation
  • Fast cache regeneration
  • Infrequent server restarts

2. Multiple Servers (Redis Shared)

Suitable Cases:
  • 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

Size Units: "10kb", "5mb", "1gb" or bytes (1024)

Redis Driver

File Driver

Knex Driver

Table Schema (auto-created):

Redis Bus Driver

Test Environment

In test environments, the memory driver is automatically used:
Internal Implementation:

Cautions

Cache Configuration Cautions:
  1. Memory Limits: Setting maxSize too large can cause OOM (Out of Memory)
  2. Redis Connection Sharing: Use the same instance for driver and bus
  3. Store Name Matching: The store option in decorators must match names defined in configuration
  4. Using L2 Without Bus: If multiple servers use only L2 (Redis), L1 won’t be synchronized

Next Steps

@cache Decorator

Apply caching to methods

Cache Invalidation

Tag-based cache invalidation

Cache Strategies

TTL, Grace, Namespace usage