Skip to content

TIP

This page mirrors packages/cache/README.md from the repository. Edit it at the source, not here.

@saasweave/cache

Redis-backed cache and rate limiting with explicit failure policies for security-sensitive state.

Behavior

TierExamplesRedis down in production
Availability cachesDashboard reads, AI usage summariesBounded per-process memory fallback
Security stateAuth/export/log rate limits, API keys, IP allowlistsfailureMode: failClosed when configured

Use resolveSecurityFailureMode() for auth rate limits, export limits, API-key lookups, and IP allowlist caches. Security paths must not silently fall back to per-process memory across replicas when REDIS_URL is configured.

ConditionAvailability cacheSecurity cache / rate limits
REDIS_URL set and Redis healthyShared Redis with tag invalidationDistributed across replicas
REDIS_URL set and Redis unavailableBounded memory fallbackFail closed (reject / cache miss to DB)
REDIS_URL unsetBounded memory fallbackFail open (single-process only)

checkRedisReady() reports unhealthy when REDIS_URL is configured and unreachable, which makes /health/ready fail in production.

In production, env validation requires REDIS_URL unless ALLOW_SINGLE_INSTANCE_FALLBACK=true is set (single-instance escape hatch).

Key exports

  • @saasweave/cache — cache helpers (cacheGet, cacheSet, cacheWrap, tag invalidation, failureMode)
  • @saasweave/cache/rate-limit — fixed-window rate limiter with failureMode
  • @saasweave/cache/redis — connection helpers and readiness checks
  • resolveSecurityFailureMode() — shared fail-closed policy helper

Environment variables

Validated in @saasweave/env (ENV_SERVER):

VariableDefaultNotes
REDIS_URLunsetRequired in production unless ALLOW_SINGLE_INSTANCE_FALLBACK=true
ALLOW_SINGLE_INSTANCE_FALLBACKfalseSingle-instance production escape hatch
CACHE_PREFIXsaasweaveRedis key namespace prefix
CACHE_DEFAULT_TTL_SECONDS300Default TTL for cache entries

Tests

bash
pnpm --filter @saasweave/cache test:unit

Released under the MIT License.