Expand description
View-level caching middleware.
Wrap a [Router] subtree with cache_page and every eligible
GET or HEAD response for that subtree is cached for the
configured TTL. Subsequent requests for the same URI + query string
get the cached response without hitting the handler.
use umbral_cache::cache_page;
use std::time::Duration;
let public = Router::new()
.route("/", get(home))
.route("/about", get(about))
.layer(cache_page(Duration::from_secs(60)));§Cache key
cache:page:GET:<host>:/path?query — method + Host header + full URI
including query string. Fragments are stripped by the browser and never
reach the server. Including the Host header prevents multi-tenant
cache-poisoning where tenant A’s cached page would otherwise be served to
requests arriving on a different Host.
§What gets cached
Only GET and HEAD responses with HTTP status 200 are stored.
The following bypass caching:
- Any method other than
GET/HEAD(POST, PUT, PATCH, DELETE). - Status code other than 200.
- Response carries
Cache-Control: no-store. - Response carries a
Set-Cookieheader (the body may be personalised). - Request carries an
umbral_sessioncookie — personalised / logged-in requests are neither served from nor written to the page cache, keeping the cache to the safe anonymous-only subset.
§Ambient cache dependency
cache_page reads the ambient super::Cache via super::ambient().
If the ambient cache has not been initialised (i.e. super::CachePlugin::init
has not been called), cache misses and stores are silently skipped —
the handler always fires normally. This is intentional: a misconfigured
cache degrades gracefully rather than returning 500s.
§Deferred
- ETag / 304 conditional caching — the current implementation always serves the full cached body. A future iteration will store and compare ETags to emit 304 Not Modified, saving bandwidth.
- Vary-header awareness (
Vary: Accept-Language, etc.). - Per-route cache key prefix customisation.
Structs§
- Cache
Page Layer tower::Layerreturned bycache_page. Wraps the inner service withCachePageService.- Cache
Page Service tower::Serviceproduced byCachePageLayer.
Constants§
- DEFAULT_
MAX_ OBJECT_ BYTES - Return a
CachePageLayerthat caches eligibleGET/HEADresponses forttl.