Struct axum_extra::extract::Cached
source · [−]pub struct Cached<T>(pub T);
Expand description
Cache results of other extractors.
Cached
wraps another extractor and caches its result in request extensions.
This is useful if you have a tree of extractors that share common sub-extractors that you only want to run once, perhaps because they’re expensive.
The cache purely type based so you can only cache one value of each type. The cache is also local to the current request and not reused across requests.
Example
use axum_extra::extract::Cached;
use axum::{
async_trait,
extract::{FromRequest, RequestParts},
body::BoxBody,
response::{IntoResponse, Response},
http::StatusCode,
};
#[derive(Clone)]
struct Session { /* ... */ }
#[async_trait]
impl<B> FromRequest<B> for Session
where
B: Send,
{
type Rejection = (StatusCode, String);
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
// load session...
}
}
struct CurrentUser { /* ... */ }
#[async_trait]
impl<B> FromRequest<B> for CurrentUser
where
B: Send,
{
type Rejection = Response;
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
// loading a `CurrentUser` requires first loading the `Session`
//
// by using `Cached<Session>` we avoid extracting the session more than
// once, in case other extractors for the same request also loads the session
let session: Session = Cached::<Session>::from_request(req)
.await
.map_err(|err| err.into_response())?
.0;
// load user from session...
}
}
// handler that extracts the current user and the session
//
// the session will only be loaded once, even though `CurrentUser`
// also loads it
async fn handler(
current_user: CurrentUser,
// we have to use `Cached<Session>` here otherwise the
// cached session would not be used
Cached(session): Cached<Session>,
) {
// ...
}
Tuple Fields
0: T
Trait Implementations
Auto Trait Implementations
impl<T> RefUnwindSafe for Cached<T> where
T: RefUnwindSafe,
impl<T> Send for Cached<T> where
T: Send,
impl<T> Sync for Cached<T> where
T: Sync,
impl<T> Unpin for Cached<T> where
T: Unpin,
impl<T> UnwindSafe for Cached<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more