pub trait CacheableResponse:
Sized
+ Send
+ 'static {
type Cached: Clone;
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 = CachePolicy<CacheValue<Self::Cached>, 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 cacheSubject- The type that predicates evaluate (for wrapper types likeResult)IntoCachedFuture- Future returned byinto_cachedFromCachedFuture- Future returned byfrom_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 cacheableErr(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§
Sourcetype Subject: CacheableResponse
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.
Sourcetype IntoCachedFuture: Future<Output = CachePolicy<Self::Cached, Self>> + Send
type IntoCachedFuture: Future<Output = CachePolicy<Self::Cached, Self>> + Send
Future type for into_cached method.
Sourcetype FromCachedFuture: Future<Output = Self> + Send
type FromCachedFuture: Future<Output = Self> + Send
Future type for from_cached method.
Required Methods§
Sourcefn cache_policy<P>(
self,
predicates: P,
config: &EntityPolicyConfig,
) -> impl Future<Output = CachePolicy<CacheValue<Self::Cached>, Self>> + Send
fn cache_policy<P>( self, predicates: P, config: &EntityPolicyConfig, ) -> impl Future<Output = CachePolicy<CacheValue<Self::Cached>, Self>> + Send
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 cacheableconfig- TTL configuration for the cached entry
Sourcefn into_cached(self) -> Self::IntoCachedFuture
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.
Sourcefn from_cached(cached: Self::Cached) -> Self::FromCachedFuture
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".