Documentation
Welcome to the Layercache documentation.
Layercache stacks memory, Redis, and disk behind a single API with stampede prevention, tag invalidation, stale-while-revalidate, and full observability.
How It Works
Every read follows the same path:
- Check L1 Memory first (fastest, in-process).
- If miss, check L2 Redis (shared across instances).
- If miss, check L3 Disk (persistent fallback).
- If all miss, run the origin fetcher once (single-flight).
- Backfill upper layers so next reads return faster.
Layercache keeps responses stable under pressure with stale serving, circuit breakers, and timeout guards.
Version 5.0 Highlights
Layercache 5.0 is a major release because it hardens cache isolation so authenticated responses are never served across users:
- Breaking:
createTrpcCacheMiddlewareandcacheGraphqlResolvernow require akeyResolver— theallowImplicitContextCachingoption is removed. Implicit path+input or argument-only keys could not distinguish authenticated callers. - Express and Hono middlewares bypass implicit URL-only caching when requests carry authentication headers (
authorization,cookie,set-cookie,x-api-key,x-session-id,x-auth-token,x-forwarded-user), so one user's authenticated response is never served to another. Provide akeyResolverto opt back in for safe keys. RedisInvalidationBussupportsrequireSignatureto fail fast when a signing secret is missing, preventing forged invalidation messages on shared Redis channels.- The test suite is split into unit and real-Redis Vitest projects, and docker-compose Redis uses the configurable
REDIS_PORT.
Read the migration guide before upgrading an existing deployment.
Version 4.0 Highlights
Layercache 4.0 makes missing values unambiguous and hardens production coordination boundaries:
- Public cache reads return
undefinedon misses while preserving intentional cachednullvalues. - Read-through fetchers cache
nullby default;cacheNullValues: falseretains legacy null-as-absence behavior. - Structured
wrap()keys use the collision-resistantj2:schema, and write ordering plus generation cleanup have finite limits. - Snapshot commits, signed invalidation, HTTP credential handling, destructive CLI patterns, and playground isolation are hardened.
- Regression coverage includes the merged scheduler, snapshot, invalidation, and epoch rollover fixes.
Read the migration guide before upgrading an existing deployment.
Quick Links
- Getting Started — Install and configure your first cache stack
- Tutorial — 10-step walkthrough of production features
- API Reference — Complete method and option documentation
- Integrations — Express, Fastify, NestJS, Hono, tRPC, GraphQL