Skip to main content

Module cache

Module cache 

Source
Expand description

In-memory response cache middleware.

CacheLayer implements Middleware and short-circuits the inner application for cacheable GET responses within their TTL. Entries are bounded by a configurable capacity; the oldest entry is evicted when the store is full and no expired entries remain.

§What is cached

  • Method: GET only; all other methods bypass the cache.
  • Status: 2xx responses (200, 201, 203, 204, 206, …).
  • Response Cache-Control: no-store or private — not cached.
  • Private or partial requests — Authorization, Cookie or Range — skip the cache entirely (no lookup, no store): their responses may differ per user or cover only part of the resource.
  • Responses that set a cookie (Set-Cookie) or stream their body (stream_file / stream_pipe, not held in memory) are not cached.
  • Request Cache-Control: no-cache — cache is bypassed, handler is called, but the fresh response is stored (revalidation).
  • How long: the response’s Cache-Control: s-maxage, else max-age, capped by the layer’s ttl; the ttl when it says neither. max-age=0 is not stored (since 18.11.0; before, every response was kept for the ttl). Hits carry an Age header.

§Example

use rust_web_server::app::App;
use rust_web_server::cache::CacheLayer;
use rust_web_server::core::New;

let app = App::new()
    .wrap(CacheLayer::memory(1000).ttl(60).vary_by_header("Accept"));

Modules§

registry
Named caches for the admin API: the binary’s own and one per config-file route that has [route.middleware.cache].

Structs§

CacheLayer
An in-memory response cache middleware.
ValueCache
Values by key, each with its own time to live, at most capacity of them (the oldest goes first when full, after anything expired). Cheap to clone; clones share the entries.

Enums§

Purge
What CacheLayer::purge removes. A cache key is the request target (path and query) plus the values of vary_by_headers, so Exact removes every variant of one target.