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-storeorprivate— not cached. - Private or partial requests —
Authorization,CookieorRange— 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, elsemax-age, capped by the layer’sttl; thettlwhen it says neither.max-age=0is not stored (since 18.11.0; before, every response was kept for thettl). Hits carry anAgeheader.
§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§
- Cache
Layer - An in-memory response cache middleware.
Enums§
- Purge
- What
CacheLayer::purgeremoves. A cache key is the request target (path and query) plus the values ofvary_by_headers, soExactremoves every variant of one target.