Skip to main content
This guide covers effective caching strategies using BentoCache in Sonamu.

What is BentoCache?

Sonamu provides powerful caching capabilities using BentoCache. Key Features:
  • Multiple drivers (memory, Redis, PostgreSQL, etc.)
  • TTL (Time To Live) configuration
  • Namespace-based management
  • Cache Warming
  • Tag-based invalidation

Cache Configuration

sonamu.config.ts Setup

Redis Driver

Multiple Drivers

@cache Decorator

Basic Usage

TTL Configuration

TTL Formats:
  • '5s': 5 seconds
  • '5m': 5 minutes
  • '1h': 1 hour
  • '1d': 1 day
  • 0: Permanent (requires invalidation)

Parameter-based Caching

Custom Cache Keys

Manual Cache Management

Using Sonamu.cache

Cache Invalidation

getOrSet Pattern

Caching Strategies

1. Read-Through Pattern

2. Write-Through Pattern

3. Cache-Aside Pattern

4. Cache Warming

Practical Examples

API Response Caching

Aggregation Data Caching

User Session Caching

Cache Invalidation Strategies

1. Time-Based (TTL)

2. Event-Based

Best Practices

1. Set Appropriate TTL

2. Cache Key Naming

3. Cache Size Management

4. Cache Warming

5. Failure Handling

Performance Comparison

Before Caching

After Caching

Over 30x performance improvement!

Checklist

For effective caching:
  • Identify frequently queried data
  • Set appropriate TTL (string format)
  • Use clear cache key naming
  • Invalidate cache on data changes
  • Monitor memory usage
  • Implement cache warming strategy
  • Add failure handling logic