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>
Available on non-WebAssembly only.
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.Source§fn layer_name(&self) -> &'static str
fn layer_name(&self) -> &'static str
Application::middleware_names): by default the type’s name
without its module path or generic parameters (CacheLayer). Since
18.28.0.Auto Trait Implementations§
impl Freeze for CacheLayer
impl RefUnwindSafe for CacheLayer
impl Send for CacheLayer
impl Sync for CacheLayer
impl Unpin for CacheLayer
impl UnsafeUnpin for CacheLayer
impl UnwindSafe for CacheLayer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more