Skip to main content

Cache

Server Source code Package

Caches HTTP responses in the transformer chain. On a cache HIT, serves the stored response directly and stops the chain. On a MISS, wraps the respond function to intercept, cache, and forward the downstream response.

Setup

Install the package:

Loading...

Configure in your code:

Loading...

How it works

  1. A request arrives and the cache transformer checks its rules in order (first match wins)
  2. The cache key is built by joining the configured ingest fields (e.g. method:path)
  3. If the key exists in the store → HIT: respond immediately with the cached response and X-Cache: HIT header, stop the chain
  4. If the key is missing → MISS: wrap the respond function so the downstream response is intercepted, stored, and forwarded with X-Cache: MISS header
  5. If no rule matches → event passes through unchanged

Settings

OptionTypeDefaultDescription
rulesCacheRule[]Cache rules, evaluated in order (first match wins)
maxSizenumber10485760 (10 MB)Maximum in-memory store size in bytes

Rule configuration

Each rule specifies when to cache, what to use as the cache key, and how long to keep it:

Loading...
OptionTypeDescription
matchMatchExpression | '*'Condition to evaluate against ingest, or '*' wildcard
keystring[]Ingest fields joined with : to form the cache key
ttlnumberTime-to-live in seconds
headersRecord<string, string>Extra headers merged into both HIT and MISS responses

Match operators

The cache transformer uses the same match expressions as the router, including all 8 operators, negation with not: true, and and/or combinators.

Examples

Cache GET requests by path

Loading...

Different TTLs per path

Loading...

Chain placement

Place the cache transformer before a responder in the chain. On HIT it stops the chain, skipping the responder entirely:

Loading...

Custom store backend

By default the cache uses an in-memory store (@walkeros/store-memory) with LRU eviction and lazy TTL. You can inject a different store via env.store:

Loading...

Any object with get(key): T | undefined and set(key, value, ttl?): void methods works as a store backend.

Behavior notes

  • LRU eviction — when the store exceeds maxSize, the least-recently-used entries are evicted first
  • Lazy TTL — expired entries are removed on the next lookup, not on a timer
  • First match wins — rules are evaluated in order; only the first matching rule applies
  • Empty key — if all key fields resolve to empty strings, the event passes through with a warning
  • No respond function — the MISS wrapper still caches the response even without env.respond (useful for pre-warming)

Next steps

💡 Need implementation support?
elbwalker offers hands-on support: setup review, measurement planning, destination mapping, and live troubleshooting. Book a 2-hour session (€399)