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).

§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"));

Structs§

CacheLayer
An in-memory response cache middleware.