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, PredicateResult};
use hitbox_core::response::ResponseCachePolicy;
use hitbox_core::value::CacheValue;
use chrono::Utc;

#[derive(Clone)]
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,
    {
        match predicates.check(self).await {
            PredicateResult::Cacheable(data) => {
                let cached = data.body.clone();
                CachePolicy::Cacheable(CacheValue::new(
                    cached,
                    config.ttl.map(|d| Utc::now() + d),
                    config.stale_ttl.map(|d| Utc::now() + d),
                ))
            }
            PredicateResult::NonCacheable(data) => CachePolicy::NonCacheable(data),
        }
    }

    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 CacheableResponse for bool

Source§

impl CacheableResponse for char

Source§

impl CacheableResponse for i8

Source§

type Cached = i8

Source§

type Subject = i8

Source§

type IntoCachedFuture = Ready<CachePolicy<i8, i8>>

Source§

type FromCachedFuture = Ready<i8>

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) -> Self::FromCachedFuture

Source§

impl CacheableResponse for i16

Source§

type Cached = i16

Source§

type Subject = i16

Source§

type IntoCachedFuture = Ready<CachePolicy<i16, i16>>

Source§

type FromCachedFuture = Ready<i16>

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) -> Self::FromCachedFuture

Source§

impl CacheableResponse for i32

Source§

type Cached = i32

Source§

type Subject = i32

Source§

type IntoCachedFuture = Ready<CachePolicy<i32, i32>>

Source§

type FromCachedFuture = Ready<i32>

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) -> Self::FromCachedFuture

Source§

impl CacheableResponse for i64

Source§

type Cached = i64

Source§

type Subject = i64

Source§

type IntoCachedFuture = Ready<CachePolicy<i64, i64>>

Source§

type FromCachedFuture = Ready<i64>

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) -> Self::FromCachedFuture

Source§

impl CacheableResponse for i128

Source§

impl CacheableResponse for isize

Source§

impl CacheableResponse for u8

Source§

type Cached = u8

Source§

type Subject = u8

Source§

type IntoCachedFuture = Ready<CachePolicy<u8, u8>>

Source§

type FromCachedFuture = Ready<u8>

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) -> Self::FromCachedFuture

Source§

impl CacheableResponse for u16

Source§

type Cached = u16

Source§

type Subject = u16

Source§

type IntoCachedFuture = Ready<CachePolicy<u16, u16>>

Source§

type FromCachedFuture = Ready<u16>

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) -> Self::FromCachedFuture

Source§

impl CacheableResponse for u32

Source§

type Cached = u32

Source§

type Subject = u32

Source§

type IntoCachedFuture = Ready<CachePolicy<u32, u32>>

Source§

type FromCachedFuture = Ready<u32>

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) -> Self::FromCachedFuture

Source§

impl CacheableResponse for u64

Source§

type Cached = u64

Source§

type Subject = u64

Source§

type IntoCachedFuture = Ready<CachePolicy<u64, u64>>

Source§

type FromCachedFuture = Ready<u64>

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) -> Self::FromCachedFuture

Source§

impl CacheableResponse for u128

Source§

impl CacheableResponse for usize

Source§

impl CacheableResponse for String

Source§

impl<T> CacheableResponse for Vec<T>
where T: CacheableResponse<Cached = T, Subject = T> + Clone + Send + 'static,

Source§

type Cached = Vec<T>

Source§

type Subject = Vec<T>

Source§

type IntoCachedFuture = Ready<CachePolicy<Vec<T>, Vec<T>>>

Source§

type FromCachedFuture = Ready<Vec<T>>

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) -> Self::FromCachedFuture

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§