Skip to content

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

MethodEndpointDescription
GET/cache/statsCache statistics
DELETE/cache/clearClear all cache
DELETE/cache/key/:keyDelete specific key
GET/cache/keysList 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:/products

Cache Headers

Responses include cache headers:

X-Cache-Hit: true
X-Cache-TTL: 300

Programmatic 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

  1. Use Redis for production (shared across instances)
  2. Set appropriate TTL per endpoint
  3. Clear cache on data mutations
  4. Monitor cache hit rates
  5. Use SQLite for single-instance deployments

Released under the ISC License.