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 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 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 runs_inline(&self, _request: &Request, _connection: &ConnectionInfo) -> bool
fn runs_inline(&self, _request: &Request, _connection: &ConnectionInfo) -> bool
In-memory only: safe on the event loop.
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.