Skip to main content

CachePolicy

Trait CachePolicy 

Source
pub trait CachePolicy:
    Default
    + Clone
    + Send
    + Sync
    + 'static {
    type Store: CacheStore + Send + Sync + 'static;

    // Provided methods
    fn cache_key(
        &self,
        ctx: &RequestContext,
        req: &Request<Body>,
    ) -> Option<String> { ... }
    fn ttl_seconds(&self) -> Option<u64> { ... }
    fn should_cache_response(&self, response: &Response) -> bool { ... }
}
Expand description

CachePolicy Trait

Defines the caching strategy for responses.

§Type Parameters

  • Self: The policy type
  • Store: The cache store implementation

§Default Behavior

By default, only GET requests are cached, with no TTL, and only successful (200 OK) responses are cached.

Required Associated Types§

Source

type Store: CacheStore + Send + Sync + 'static

The cache store type used by this policy

Provided Methods§

Source

fn cache_key(&self, ctx: &RequestContext, req: &Request<Body>) -> Option<String>

Generates a cache key for the given request.

Return None to skip caching for this request. Default implementation caches all GET requests.

Source

fn ttl_seconds(&self) -> Option<u64>

Returns the time-to-live in seconds for cached responses.

Return None for no expiration.

Source

fn should_cache_response(&self, response: &Response) -> bool

Determines whether a response should be cached.

Default implementation caches only 200 OK responses.

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.

Implementors§

Source§

impl<S> CachePolicy for DefaultCachePolicy<S>
where S: CacheStore + Send + Sync + 'static,

Source§

type Store = S