pub struct CacheLayer { /* private fields */ }Expand description
An in-memory response cache middleware.
Construct with CacheLayer::memory and configure with the builder methods
ttl and vary_by_header.
CacheLayer is cheaply Clone-able — clones share the same underlying
store, which lets you keep a handle for cache stats and invalidation while
the other clone is used as middleware.
Implementations§
Source§impl CacheLayer
impl CacheLayer
Sourcepub fn memory(capacity: usize) -> Self
pub fn memory(capacity: usize) -> Self
Create a new in-memory cache bounded to capacity entries.
Default TTL is 60 seconds. Adjust with .ttl().
Sourcepub fn purge_token(self, token: impl Into<String>) -> Self
pub fn purge_token(self, token: impl Into<String>) -> Self
Accept the HTTP PURGE method on the routes behind this layer,
authenticated by Authorization: Bearer <token> (since 18.11.0).
PURGE /products/42 removes that target (every Vary variant);
PURGE /products/* removes the prefix. The answer is
{"purged":<n>}; a missing or wrong token gets 401. Without a
token, PURGE passes through to the application as before.
Sourcepub fn disk(dir: impl Into<PathBuf>, max_bytes: u64) -> Result<Self>
pub fn disk(dir: impl Into<PathBuf>, max_bytes: u64) -> Result<Self>
A cache kept in files under dir (created if missing), bounded to
max_bytes in total, oldest dropped first (since 18.11.0). Entries
survive a restart: the directory is indexed when the layer is built,
expired and unreadable files removed. Same rules as
memory for what is cached and for how long.
use rust_web_server::cache::CacheLayer;
let layer = CacheLayer::disk("/var/cache/rws/api", 512 * 1024 * 1024)?.ttl(600);Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Current number of entries in the store (including potentially expired ones that haven’t been purged yet).
Sourcepub fn stored_bytes(&self) -> Option<u64>
pub fn stored_bytes(&self) -> Option<u64>
Bytes held by a disk cache’s files; None for a memory cache.
Sourcepub fn vary_by_header(self, name: &str) -> Self
pub fn vary_by_header(self, name: &str) -> Self
Include a request header in the cache key so that different values of that header produce separate cache entries.
Header name matching is case-insensitive. Call multiple times to vary by more than one header.
use rust_web_server::cache::CacheLayer;
let layer = CacheLayer::memory(500)
.vary_by_header("Accept")
.vary_by_header("Accept-Language");Trait Implementations§
Source§impl Clone for CacheLayer
impl Clone for CacheLayer
Source§impl Middleware for CacheLayer
impl Middleware for CacheLayer
Source§fn accepts_streamed_body(
&self,
_request: &Request,
_connection: &ConnectionInfo,
) -> bool
fn accepts_streamed_body( &self, _request: &Request, _connection: &ConnectionInfo, ) -> bool
Headers only: a streamed body passes through untouched.
Source§fn runs_inline(&self, _request: &Request, _connection: &ConnectionInfo) -> bool
fn runs_inline(&self, _request: &Request, _connection: &ConnectionInfo) -> bool
In-memory: safe on the event loop. A disk store reads and writes files, so it runs on the blocking pool.
fn handle( &self, request: &Request, connection: &ConnectionInfo, next: &dyn Application, ) -> Result<Response, String>
Source§fn is_passthrough(
&self,
_request: &Request,
_connection: &ConnectionInfo,
) -> bool
fn is_passthrough( &self, _request: &Request, _connection: &ConnectionInfo, ) -> bool
true when handle would do nothing for request but call next
(e.g. a rate limit that isn’t configured). A WithMiddleware stack
offers the inner application’s execute_async only if every layer
passes the request through. Default false.Source§fn answers_streamed_body(
&self,
_request: &Request,
_connection: &ConnectionInfo,
) -> bool
fn answers_streamed_body( &self, _request: &Request, _connection: &ConnectionInfo, ) -> bool
true when this layer answers request itself — it doesn’t call
next — and reads its body as a stream (a proxy forwarding it as it
arrives). The body is then streamed whatever the layers after it and
the application would say. Requires accepts_streamed_body. Since
18.3.0.