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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response. Read more

Perform the extraction.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more