Caching
Backflow supports Redis, SQLite, and in-memory caching.
Configuration
Redis Cache
json
{
"cache": {
"enabled": true,
"provider": "redis",
"redis": {
"url": "{{env.REDIS_URL}}"
},
"ttl": 3600,
"maxSize": 10000
}
}SQLite Cache
json
{
"cache": {
"enabled": true,
"provider": "sqlite",
"sqlite": {
"dbPath": "./cache.db"
},
"ttl": 3600,
"maxSize": 10000
}
}Memory Cache
json
{
"cache": {
"enabled": true,
"provider": "memory",
"maxSize": 1000,
"maxMemoryMB": 100
}
}Route-Level Caching
json
{
"path": "/products",
"method": "get",
"cache": {
"enabled": true,
"ttl": 300
},
"supabaseQueries": [{
"table": "products",
"operation": "select"
}]
}Cache Keys
Cache keys are generated from:
- Request path
- Query parameters
- Tenant ID (if applicable)
Cache Management Endpoints
| Method | Endpoint | Description |
|---|---|---|
| GET | /cache/stats | Cache statistics |
| DELETE | /cache/clear | Clear all cache |
| DELETE | /cache/key/:key | Delete specific key |
| GET | /cache/keys | List cached keys |
LLM Response Caching
LLM responses are cached automatically:
json
{
"cache": {
"enabled": true,
"llm": {
"enabled": true,
"ttl": 86400
}
}
}Tenant Cache Isolation
Each tenant has isolated cache:
cache:tenant:abc123:/products
cache:tenant:xyz789:/productsCache Headers
Responses include cache headers:
X-Cache-Hit: true
X-Cache-TTL: 300Programmatic Cache
typescript
import { getCacheService } from './lib/cache/cache-service.js';
const cache = getCacheService();
// Set value
await cache.set('key', { data: 'value' }, 3600);
// Get value
const value = await cache.get('key');
// Delete
await cache.delete('key');
// Clear pattern
await cache.clearPattern('prefix:*');Best Practices
- Use Redis for production (shared across instances)
- Set appropriate TTL per endpoint
- Clear cache on data mutations
- Monitor cache hit rates
- Use SQLite for single-instance deployments