Skip to main content

CacheableResponse

Trait CacheableResponse 

Source
pub trait CacheableResponse
where Self: Sized + Send + 'static, Self::Cached: Clone,
{ type Cached; type Subject: CacheableResponse; type IntoCachedFuture: Future<Output = CachePolicy<Self::Cached, Self>> + Send; type FromCachedFuture: Future<Output = Self> + Send; // Required methods fn cache_policy<P>( self, predicates: P, config: &EntityPolicyConfig, ) -> impl Future<Output = ResponseCachePolicy<Self>> + Send where P: Predicate<Subject = Self::Subject> + Send + Sync; fn into_cached(self) -> Self::IntoCachedFuture; fn from_cached(cached: Self::Cached) -> Self::FromCachedFuture; }
Expand description

Trait for response types that can be cached.

This trait defines how responses are converted to and from their cached representation. Implementations must provide methods for:

  • Determining if a response should be cached (cache_policy)
  • Converting to the cached format (into_cached)
  • Reconstructing from cached data (from_cached)

§Associated Types

  • Cached - The serializable representation stored in cache
  • Subject - The type that predicates evaluate (for wrapper types like Result)
  • IntoCachedFuture - Future returned by into_cached
  • FromCachedFuture - Future returned by from_cached

§Blanket Implementation

A blanket implementation is provided for Result<T, E> where T: CacheableResponse. This allows:

  • Ok(response) to be cached if the inner response is cacheable
  • Err(error) to always pass through without caching

§Example Implementation

use hitbox_core::{CacheableResponse, CachePolicy, EntityPolicyConfig};
use hitbox_core::predicate::Predicate;

struct MyResponse {
    body: String,
    status: u16,
}

impl CacheableResponse for MyResponse {
    type Cached = String;
    type Subject = Self;
    type IntoCachedFuture = std::future::Ready<CachePolicy<String, Self>>;
    type FromCachedFuture = std::future::Ready<Self>;

    async fn cache_policy<P>(
        self,
        predicates: P,
        config: &EntityPolicyConfig,
    ) -> ResponseCachePolicy<Self>
    where
        P: Predicate<Subject = Self::Subject> + Send + Sync
    {
        // Implementation details...
    }

    fn into_cached(self) -> Self::IntoCachedFuture {
        std::future::ready(CachePolicy::Cacheable(self.body))
    }

    fn from_cached(cached: String) -> Self::FromCachedFuture {
        std::future::ready(MyResponse { body: cached, status: 200 })
    }
}

Required Associated Types§

Source

type Cached

The serializable type stored in cache.

Source

type Subject: CacheableResponse

The type that response predicates evaluate.

For simple responses, this is Self. For wrapper types like Result<T, E>, this is the inner type T.

Source

type IntoCachedFuture: Future<Output = CachePolicy<Self::Cached, Self>> + Send

Future type for into_cached method.

Source

type FromCachedFuture: Future<Output = Self> + Send

Future type for from_cached method.

Required Methods§

Source

fn cache_policy<P>( self, predicates: P, config: &EntityPolicyConfig, ) -> impl Future<Output = ResponseCachePolicy<Self>> + Send
where P: Predicate<Subject = Self::Subject> + Send + Sync,

Determine if this response should be cached.

Applies predicates to determine cacheability, then converts cacheable responses to their cached representation with TTL metadata.

§Arguments
  • predicates - Predicates to evaluate whether the response is cacheable
  • config - TTL configuration for the cached entry
Source

fn into_cached(self) -> Self::IntoCachedFuture

Convert this response to its cached representation.

Returns Cacheable with the serializable data, or NonCacheable if the response should not be cached.

Source

fn from_cached(cached: Self::Cached) -> Self::FromCachedFuture

Reconstruct a response from cached data.

Creates a new response instance from previously cached data.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T, E> CacheableResponse for Result<T, E>
where T: CacheableResponse + 'static, E: Send + 'static, T::Cached: Send,

Source§

type Cached = <T as CacheableResponse>::Cached

Source§

type Subject = T

Source§

type IntoCachedFuture = ResultIntoCachedFuture<T, E>

Source§

type FromCachedFuture = ResultFromCachedFuture<T, E>

Source§

async fn cache_policy<P>( self, predicates: P, config: &EntityPolicyConfig, ) -> ResponseCachePolicy<Self>
where P: Predicate<Subject = Self::Subject> + Send + Sync,

Source§

fn into_cached(self) -> Self::IntoCachedFuture

Source§

fn from_cached(cached: Self::Cached) -> Self::FromCachedFuture

Implementors§