# Stores (https://lazy-layers-cache.vercel.app/docs/reference/stores)

> The CacheStore interface, MemoryStore, RedisStore, and what it takes to write your own.



A store is anything that implements `CacheStore`. Both layers take one: `l1` defaults to a `MemoryStore` built from your options, and `l2` has no default at all.

## The CacheStore interface [#the-cachestore-interface]

Eight methods, all asynchronous. This is the complete contract.

```ts
interface CacheStore<K extends CacheKey, V> {
  set(key: K, value: V, options?: CacheOptions): Promise<void>
  get(key: K): Promise<V | undefined>
  getOrSet(key: K, loader: CacheLoader<V>, options?: CacheOptions): Promise<V | undefined>
  has(key: K): Promise<boolean>
  delete(key: K): Promise<void>
  deleteByPattern(pattern: string): Promise<void>
  clear(): Promise<void>
  size(): Promise<number>
}
```

`CacheKey` is `string | number`. `CacheLoader<V>` is `(context?: { signal: AbortSignal }) => Promise<V | undefined>`.

`undefined` is the miss signal throughout. A store returning `undefined` from `get` means "not here", which is why a cached `undefined` is handled by the negative cache rather than by the store.

## MemoryStore [#memorystore]

The default L1: a typed-array slot table with a pluggable eviction policy. In the default `encoded` mode it owns tagged serializer buffers and every read decodes a fresh value, so caller mutation cannot change the retained entry. In `reference` mode it keeps the object itself and returns it without decoding.

```ts
new MemoryStore<K extends CacheKey, V>(options?: CacheOptions)
```

Two type parameters, key first. It takes the same `CacheOptions` the cache takes and resolves L1 lifetime, count, memory, pressure, and admission settings.

<CodeGroup>
  ```js title="src/cache/l1.js"
  import { MemoryStore } from 'lazy-layers-cache'

  export const l1 = new MemoryStore({
    levels: { L1: { maxEntries: 10_000, ttlMs: 30_000 } },
  })
  ```

  ```ts title="src/cache/l1.ts"
  import { MemoryStore } from 'lazy-layers-cache'
  import type { User } from '../types/user.js'

  export const l1 = new MemoryStore<string, User>({
    levels: { L1: { maxEntries: 10_000, ttlMs: 30_000 } },
  })
  ```
</CodeGroup>

You rarely construct one yourself. The cache builds it for you from the options you already passed, unless you set `l1` explicitly or set `l1: false`.

### L1 limits and policy [#l1-limits-and-policy]

<ParamField path="levels.L1.maxEntries" type="number" default="1000">
  Becomes `max`. A count of entries, not bytes.
</ParamField>

<ParamField path="levels.L1.ttlMs" type="number" default="3600000">
  Becomes `ttl`. Falls back to the cache-wide `ttlMs`, then to one hour.
</ParamField>

<ParamField path="levels.L1.maxMemory" type="MemoryLimit" default="20%">
  Hard ceiling shared by built-in stores in one process. Percentages resolve against effective host or container memory. A value above 75% of effective memory is lowered to 75%, with a `LAZY_LAYERS_MAX_MEMORY` warning.
</ParamField>

<ParamField path="levels.L1.maxProcessMemory" type="MemoryLimit" default="effective memory">
  Resident memory (RSS) the whole process should stay under. Above 90% of it the cache shrinks; above 100% it also refuses new L1 writes. Unlike `maxMemory`, this reacts to everything the process holds, including memory outside the V8 heap. Defaults to effective memory (the container limit, or host memory); set it lower to leave headroom.
</ParamField>

<ParamField path="levels.L1.minMemory" type="MemoryLimit">
  Optional lower bound for the adaptive target during pressure.
</ParamField>

<ParamField path="levels.L1.autoEvict.enabled" type="boolean" default="true">
  Samples free memory, Linux pressure, the V8 heap and RSS every 5 seconds, and shrinks or gradually recovers the active target. Disabling it stops the periodic sampling only: writes still evict to fit `maxEntries` and `maxMemory`, and still check the V8 heap and RSS, starting sampling again if either nears its limit. See [memory limits and pressure](/docs/concepts/eviction#memory-limits-and-pressure).
</ParamField>

<ParamField path="levels.L1.admission" type="{ enabled?: boolean; maxEntryBytes?: number }">
  Rejects oversized entries before they displace the hot set and uses bounded history when the cache must choose whether to admit a new value.
</ParamField>

That is the whole surface.

* **The replacement policy is configurable.** `levels.L1.policy` selects `s3fifo` (default), `lru`, `sieve`, `fifo`, or your own; see [eviction and value modes](/docs/concepts/eviction). Bounded frequency history affects admission, not victim ordering.
* **Byte accounting is explicit.** Encoded payloads, key and metadata overhead, and fail-safe snapshots share the configured budget. An encoded entry is charged its payload, its key at two bytes per character, and 96 bytes of overhead for records up to 2 KiB, which are kept as compact one-byte strings, or 160 bytes for larger records kept as a Buffer. Fail-safe snapshots of a compact record share it rather than copying it. RSS is watched separately, through `maxProcessMemory`, which defaults to effective memory (the container limit, or host memory).

Keep `maxEntries` as a count defense for tiny keys and use `maxMemory` for cache-owned retained bytes. Under Node clustering every worker has an independent process-local budget unless the deployment divides a host allowance explicitly.

### TTL on write [#ttl-on-write]

`set` resolves the lifetime most specific first: the per-call `levels.L1.ttlMs`, then the per-call `ttlMs`, then the store's constructor `levels.L1.ttlMs`, then its constructor `ttlMs`, then one hour.

### The rest of the behaviour [#the-rest-of-the-behaviour]

`deleteByPattern` walks every key in L1 and glob-matches it, so its cost is the size of L1, not the size of the match. `size()` and `stats()` first remove expired entries, then report the live count; `stats()` also reports the policy and its counters. `getSync(key)`, `setSync(key, value, options?)` and `hasSync(key)` are synchronous versions of `get`, `set` and `has`, for using the store directly as an in-process cache with no promise per call.

`MemoryStore` implements `InspectableStore`. Its `inspect` reads values with `peek()`, so opening the dashboard never changes recency or brings forward an eviction.

## Cloudflare KV stores [#cloudflare-kv-stores]

For Node.js, `CloudflareKVStore<V>` implements `CacheStore<CacheKey, V>` with a Workers KV binding-compatible namespace. `CloudflareKVRestNamespace` supplies that namespace through Cloudflare's REST API outside Workers. For Workers, import `CloudflareWorkerKVStore<V>` or `CloudflareWorkerCache<V>` from `lazy-layers-cache/cloudflare`.

All KV stores support `getOrSet`, `deleteByPattern`, `clear`, and `size`. A pattern delete lists keys by literal prefix, then filters and deletes matching names. The Worker store rejects scans over 400 keys by default before deleting, with `maxPatternScanKeys` configurable up to 900. KV listings are eventually consistent, so pattern deletion is best-effort under concurrent writes. `size` counts listed physical records, including records whose logical TTL has expired but whose minimum KV retention has not. Neither KV store implements distributed locks or `levels.L2.maxEntries`.

The two runtimes share an `LLK1` envelope with a logical expiry and the existing HC1 MessagePack format. The Node store and `setupCache({ kv })` select Worker-compatible HC1 serialization by default. Both KV stores try gzip for packed values of at least 1 KiB and keep it only when gzip saves at least 15% of packed bytes; `compression: 'none'` disables new compressed writes. Raw and compressed KV records use `HC1M` and `HC1G` and are readable from either runtime. KV operation charges do not shrink with the payload. `CloudflareQueueInvalidationPublisher` can publish application invalidations from either runtime, and a dedicated Queue consumer repeats KV deletion. See [Cloudflare KV setup](/docs/setups/cloudflare-kv) for the system design and limits.

## RedisStore [#redisstore]

The shared L2. It takes an `ioredis` client you already built, so connection settings, TLS and cluster configuration stay yours.

```ts
new RedisStore<V>(redis: Redis, options?: RedisStoreOptions)
```

<Warning>
  **`RedisStore` declares one type parameter, not two.** The class is `RedisStore<V> implements CacheStore<CacheKey, V>`, so the key type is fixed to `CacheKey` and the only thing you name is the value.

  Write `new RedisStore<User>(redis, options)`. `RedisStore<string, User>` does not compile.
</Warning>

<CodeGroup>
  ```js title="src/cache/index.js"
  import { LazyLayersCache, RedisStore } from 'lazy-layers-cache'
  import { redis } from './redis.js'

  export const cache = new LazyLayersCache({
    ttlMs: 5 * 60 * 1000,
    l2: new RedisStore(redis, {
      prefix: 'app:cache:',
      deleteStrategy: 'unlink',
      scanCount: 500,
    }),
  })
  ```

  ```ts title="src/cache/index.ts"
  import { LazyLayersCache, RedisStore, type LazyLayersCacheOptions } from 'lazy-layers-cache'
  import type { User } from '../types/user.js'
  import { redis } from './redis.js'

  // One type parameter: the value. The key type is fixed to CacheKey.
  const options: LazyLayersCacheOptions<string, User> = {
    ttlMs: 5 * 60 * 1000,
    l2: new RedisStore<User>(redis, {
      prefix: 'app:cache:',
      deleteStrategy: 'unlink',
      scanCount: 500,
    }),
  }

  export const cache = new LazyLayersCache<string, User>(options)
  ```
</CodeGroup>

### RedisStoreOptions [#redisstoreoptions]

`RedisStoreOptions` extends `CacheOptions`, so a store can carry its own `ttlMs` and `levels` policy independent of the cache. It also controls the Redis key layout, optional namespace catalogue, scans, and delete strategy.

<ParamField path="prefix" type="string" default="cache:">
  Namespaces the logical keys. In the v2 layout, it follows the `lazy-layers:v2:{encoded-logical-key}:` prefix. Two applications sharing a Redis instance with the same prefix will invalidate each other's keys.
</ParamField>

<ParamField path="indexKey" type="string" default="`${prefix}__index`">
  The optional sorted set that tracks live keys, scored by their expiry deadline. Derived from `prefix` unless you set it. Change it only if it collides with something you already store.
</ParamField>

<ParamField path="useIndex" type="boolean" default="false">
  Maintain the index, so pattern deletes and `size` use `ZSCAN` and `ZCARD` instead of walking the keyspace. It is also selected when `levels.L2.maxEntries` is set.

  Leave it off for Redis-native TTL and operator-managed eviction. Turning it on adds a `ZADD` per write and avoids a full `SCAN` for pattern deletes and `size()`.
</ParamField>

<ParamField path="scanCount" type="number" default="1000">
  The `COUNT` hint on `SCAN`, `ZSCAN` and the streaming variants. Higher finishes a sweep in fewer round trips and blocks the Redis event loop for longer on each one.
</ParamField>

<ParamField path="batchSize" type="number" default="500">
  Keys per delete pipeline, and the batch size for accumulating matches during an indexed pattern delete. Raising it turns a large invalidation into one long stall instead of several short ones.
</ParamField>

<ParamField path="deleteStrategy" type="'unlink' | 'del'" default="unlink">
  `unlink` frees memory on a background thread, so a large delete does not block the Redis event loop. Any value other than the exact string `'del'` resolves to `unlink`. Choose `del` only for a Redis older than 4.0.
</ParamField>

<ParamField path="keyLayout" type="'auto' | 'legacy' | 'v2'" default="'auto'">
  `auto` uses the v2 per-entry key layout when the client exposes `defineCommand` and otherwise keeps the legacy layout. Set `v2` only after the namespace migration is complete. Set `legacy` while old writers still use the legacy namespace.
</ParamField>

### The key layout [#the-key-layout]

The legacy layout has three shapes under your prefix.

| Shape                  | Purpose                                                          |
| ---------------------- | ---------------------------------------------------------------- |
| `{prefix}{key}`        | One cached value, serialized, with a `PX` expiry                 |
| `{prefix}__index`      | Optional sorted set of live key names, scored by expiry deadline |
| `{prefix}__lock:{key}` | Distributed lock token, held for the lock TTL                    |

The v2 layout stores values as `lazy-layers:v2:{encoded-logical-key}:{prefix}{key}` and leases as `lazy-layers:v2:{encoded-logical-key}:{prefix}__lock:{key}`. The hash tag keeps one value and its lease in the same Redis Cluster slot while unrelated keys still shard independently. It does not dual-read the legacy layout. During a mixed-version rollout, set `keyLayout: 'legacy'` on upgraded writers while older writers remain. Drain or explicitly backfill the legacy namespace before moving every writer to v2. Update Redis ACL key patterns to cover the v2 prefix as described in [production setup](/docs/setups/production#redis-6-through-8-migration-and-acls).

When `useIndex` is off, `inspect` filters out internal keys so the dashboard does not show bookkeeping keys.

### What each operation actually does [#what-each-operation-actually-does]

`set` uses a token-fencing script with v2, so a direct write removes an outstanding loader lease in the same Redis operation. The legacy layout pipelines `SET key value PX ttl` with an optional `ZADD`. When `levels.L2.maxEntries` selects the index, the store trims the oldest expiry entries after the write. This is a bounded, best-effort cache policy under concurrent writers, not a transaction quota.

`get` uses one script execution to read the value and remaining TTL with v2. The legacy layout reads binary data with `getBuffer()` and its TTL separately. A miss removes that key from the optional index, so an expired key stops being counted by `size()`.

`delete` and `deleteByPattern` batch into pipelines of `batchSize`, issuing `UNLINK` or `DEL` alongside a `ZREM` on the index. With the index on, a pattern delete streams `ZSCAN`. With it off, it streams a keyspace `SCAN`.

`clear()` is `deleteByPattern('*')` under your prefix, so it does not touch keys another application put in the same Redis.

`size()` is `ZCARD` when the index is on. Without it, the store performs a full namespace `SCAN`.

### The two optional capabilities [#the-two-optional-capabilities]

`RedisStore` implements both, which is why it is the store the dashboard and the distributed lock are written against.

`InspectableStore` gives you `inspect`, used by the observability dashboard. It advances one `SCAN` or `ZSCAN` step at a time and pipelines `PTTL` plus the value fetch, so it never blasts the keyspace and never runs unless a dashboard tab is open.

`acquireLock`, `renewLock` and `releaseLock` back `distributedLock`. Acquire is `SET NX PX` on the lease key. Renewal and release compare the token in a script. `publishIfOwner` checks that same token and writes the encoded value with its TTL in one Redis operation. An expired owner cannot renew, release, or publish over another owner's value. HybridCache manages renewal automatically.

## Introspection [#introspection]

<ParamField path="InspectableStore" type="interface">
  One method: `inspect(options?: StoreInspectOptions): Promise<StoreInspection>`. Both shipped stores implement it.
</ParamField>

```ts
interface StoreInspectOptions {
  cursor?: string
  limit?: number
  match?: string
  includeValues?: boolean
  maxValueBytes?: number
}

interface StoreInspection {
  size: number
  cursor?: string
  keys: KeyInspection[]
}

interface KeyInspection {
  key: string
  ttlRemainingMs?: number
  serializedBytes: number
  deserializedBytes: number
  compressionRatio: number
  encoding: 'msgpack' | 'msgpack-gzip' | 'json' | 'legacy'
  value?: unknown
  truncated?: boolean
}
```

`limit` defaults to `100` in both stores, and `includeValues` defaults to `true` via a `!== false` check. `maxValueBytes` defaults to `262144`, which is 256 KB, and a value over it is reported with `truncated: true` instead of its contents. An absent `cursor` in the result means the scan is finished.

Both built-in stores retain tagged encoded buffers, so `serializedBytes` is the payload actually stored and inspection does not re-serialize it. `deserializedBytes` comes from write-time serializer statistics when available; unknown legacy or directly injected encoded values are left truncated instead of being expanded only for inspection.

### isInspectableStore [#isinspectablestore]

A runtime type guard, exported from the package. Use it before calling `inspect` on a store you did not construct.

<CodeGroup>
  ```js title="src/cache/browse.js"
  import { isInspectableStore } from 'lazy-layers-cache'

  export async function firstPage(store, match) {
    if (!isInspectableStore(store)) {
      return { size: 0, keys: [] }
    }

    return store.inspect({ match, limit: 50, includeValues: false })
  }
  ```

  ```ts title="src/cache/browse.ts"
  import { isInspectableStore, type StoreInspection } from 'lazy-layers-cache'

  export async function firstPage(store: unknown, match: string): Promise<StoreInspection> {
    if (!isInspectableStore(store)) {
      return { size: 0, keys: [] }
    }

    return store.inspect({ match, limit: 50, includeValues: false })
  }
  ```
</CodeGroup>

The guard is a single `typeof store?.inspect === 'function'` check. A store without `inspect` still works everywhere else, and the dashboard simply shows nothing for that layer.

## Writing your own store [#writing-your-own-store]

Implement the eight `CacheStore` methods and pass the instance as `l1` or `l2`.

<CodeGroup>
  ```js title="src/cache/mapStore.js"
  // A minimal store with no eviction. Real backends need a bound.
  export class MapStore {
    constructor() {
      this.entries = new Map()
    }

    async set(key, value) {
      this.entries.set(key, value)
    }

    async get(key) {
      return this.entries.get(key)
    }

    async getOrSet(key, loader, options) {
      const cached = await this.get(key)
      if (cached !== undefined) return cached

      const value = await loader()
      if (value === undefined) return undefined

      await this.set(key, value, options)
      return value
    }

    async has(key) {
      return this.entries.has(key)
    }

    async delete(key) {
      this.entries.delete(key)
    }

    async deleteByPattern(pattern) {
      const regex = new RegExp(`^${pattern.split('*').map(escapeRegExp).join('.*')}$`)

      for (const key of this.entries.keys()) {
        if (regex.test(String(key))) this.entries.delete(key)
      }
    }

    async clear() {
      this.entries.clear()
    }

    async size() {
      return this.entries.size
    }
  }

  function escapeRegExp(part) {
    return part.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
  }
  ```

  ```ts title="src/cache/mapStore.ts"
  import type { CacheLoader, CacheOptions, CacheStore } from 'lazy-layers-cache'

  // A minimal store with no eviction. Real backends need a bound.
  export class MapStore<V> implements CacheStore<string, V> {
    private readonly entries = new Map<string, V>()

    async set(key: string, value: V): Promise<void> {
      this.entries.set(key, value)
    }

    async get(key: string): Promise<V | undefined> {
      return this.entries.get(key)
    }

    async getOrSet(key: string, loader: CacheLoader<V>, options?: CacheOptions): Promise<V | undefined> {
      const cached = await this.get(key)
      if (cached !== undefined) return cached

      const value = await loader()
      if (value === undefined) return undefined

      await this.set(key, value)
      return value
    }

    async has(key: string): Promise<boolean> {
      return this.entries.has(key)
    }

    async delete(key: string): Promise<void> {
      this.entries.delete(key)
    }

    async deleteByPattern(pattern: string): Promise<void> {
      const regex = new RegExp(`^${pattern.split('*').map(escapeRegExp).join('.*')}$`)

      for (const key of this.entries.keys()) {
        if (regex.test(key)) this.entries.delete(key)
      }
    }

    async clear(): Promise<void> {
      this.entries.clear()
    }

    async size(): Promise<number> {
      return this.entries.size
    }
  }

  function escapeRegExp(part: string): string {
    return part.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
  }
  ```
</CodeGroup>

Then hand it to a layer. `getOrSet` on the cache goes through your store on the way to the loader, so nothing above changes.

<CodeGroup>
  ```js title="src/users/repository.js"
  import { cache } from '../cache/index.js'
  import { db } from '../db.js'

  export async function getUser(id) {
    return cache.getOrSet(`user:${id}`, async ({ signal } = {}) => {
      const row = await db.users.findById(id, { signal })
      return row ?? undefined
    })
  }
  ```

  ```ts title="src/users/repository.ts"
  import { cache } from '../cache/index.js'
  import type { User } from '../types/user.js'
  import { db } from '../db.js'

  export async function getUser(id: string): Promise<User | undefined> {
    return cache.getOrSet(`user:${id}`, async ({ signal } = {}) => {
      const row = await db.users.findById(id, { signal })
      return row ?? undefined
    })
  }
  ```
</CodeGroup>

### Things to get right [#things-to-get-right]

**Return `undefined` for a miss, never `null`.** The cache treats `undefined` as "not present" everywhere. A `null` return counts as a hit and is served to your callers.

**Only L2 is wrapped in a circuit breaker.** Every L2 call goes through a guard that catches the failure, records it against `resilience.l2CircuitBreaker`, emits `l2:error` and falls back. Calls to your L1 store are awaited directly with no guard at all, so an L1 store that throws propagates that error straight to the caller of `getOrSet`. If you are writing an L1 store, handle your own failures and degrade to a miss.

**Add the optional capabilities you can support.** Both are detected at runtime and neither is required.

| Capability         | Methods                                                     | Unlocks                                                       |
| ------------------ | ----------------------------------------------------------- | ------------------------------------------------------------- |
| `InspectableStore` | `inspect(options?)`                                         | Key browsing in the observability dashboard                   |
| Distributed lock   | `acquireLock(key, token, ttlMs)`, `releaseLock(key, token)` | `distributedLock`, which serializes cold loads across servers |

Custom stores may also provide `renewLock(key, token, ttlMs): Promise<boolean>`. It must atomically extend only a live lock owned by that token, returning `false` after ownership is lost. Without this optional method, the load is bounded by the original lease.

The lock check is structural: an object with both methods as functions satisfies it. `acquireLock` must return `true` only when it actually took the lock, and `releaseLock` must compare the token before deleting, or a slow holder will release a lock somebody else now owns.

**Respect the options argument on `set`.** A store that ignores `options.ttlMs` and `options.levels` silently discards every per-call TTL override the cache passes down.

## Where to next [#where-to-next]

<CardGroup cols="2">
  <Card title="Configuration reference" icon="sliders" href="/docs/reference/configuration#lifetime-and-layers">
    How `l1`, `l2` and `levels` resolve, and the TTL precedence a store has to honour.
  </Card>

  <Card title="Layers" icon="layer-group" href="/docs/concepts/layers">
    Why L1 and L2 have different jobs, and how a read walks them.
  </Card>

  <Card title="Serialization" icon="box" href="/docs/concepts/serialization">
    What `RedisStore` actually writes into Redis, and how the encoding prefix is chosen.
  </Card>
</CardGroup>
