Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Caching Middlewares

  • cache — in-memory response caching with TTL

cache

Caches responses in memory with TTL support.

x-barbacane-middlewares:
  - name: cache
    config:
      ttl: 300
      vary:
        - Accept-Language
        - Accept-Encoding
      methods:
        - GET
        - HEAD
      cacheable_status:
        - 200
        - 301

Configuration

PropertyTypeDefaultDescription
ttlinteger300Cache duration (seconds)
varyarray[]Headers that vary cache key
methodsarray["GET", "HEAD"]HTTP methods to cache
cacheable_statusarray[200, 301]Status codes to cache

Cache key

The cache key is computed from:

  • HTTP method
  • Request path and query string
  • Values of the headers listed in vary

Storage rules

A response is stored only when its status is in cacheable_status, and never when its Cache-Control carries no-store or private.

A response to a request carrying Authorization is stored only when it explicitly allows a shared cache to reuse it, through public, s-maxage or must-revalidate (RFC 9111 §3.5), or when vary lists authorization, which keys each entry to its credential. Without either, one user’s response could be served to another.

Entries expire after ttl seconds. max-age and no-cache in the response are not interpreted.