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::FromRequestParts,
response::{IntoResponse, Response},
http::{StatusCode, request::Parts},
};
#[derive(Clone)]
struct Session { /* ... */ }
#[async_trait]
impl<S> FromRequestParts<S> for Session
where
S: Send + Sync,
{
type Rejection = (StatusCode, String);
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
// load session...
}
}
struct CurrentUser { /* ... */ }
#[async_trait]
impl<S> FromRequestParts<S> for CurrentUser
where
S: Send + Sync,
{
type Rejection = Response;
async fn from_request_parts(parts: &mut Parts, state: &S) -> 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_parts(parts, state)
.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§
Source§impl<S, T> FromRequestParts<S> for Cached<T>
impl<S, T> FromRequestParts<S> for Cached<T>
Source§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::Rejection
If the extractor fails it’ll use this “rejection” type. A rejection is
a kind of error that can be converted into a response.
Auto Trait Implementations§
impl<T> Freeze for Cached<T>where
T: Freeze,
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§
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<S, T> FromRequest<S, ViaParts> for T
impl<S, T> FromRequest<S, ViaParts> for T
Source§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::Rejection
If the extractor fails it’ll use this “rejection” type. A rejection is
a kind of error that can be converted into a response.