Skip to main content

Cache

Trait Cache 

Source
pub trait Cache:
    Send
    + Sync
    + 'static {
    // Required methods
    fn get_value(&self, key: &str) -> Option<Arc<dyn Any + Send + Sync>>;
    fn insert_value(&self, key: &str, value: Arc<dyn Any + Send + Sync>);
    fn invalidate(&self, key: &str);
    fn clear(&self);

    // Provided method
    fn insert_raw_bytes(
        &self,
        _key: &str,
        _bytes: Vec<u8>,
        _ttl: Option<Duration>,
    ) { ... }
}
Expand description

A type-erased, thread-safe cache store.

Implementations must be Send + Sync so they can be shared across handlers and tasks. Values are stored as Arc<dyn Any> for type erasure, allowing a single cache instance to store heterogeneous types from different #[cached] functions.

Use the free functions get / insert for in-process-only values, or get_cached / insert_cached for types that also implement serde, which is required for cross-replica backends like Redis. CacheResponseLayer uses the serde-aware path so HTTP response caching works with both in-process and raw-byte backends.

Required Methods§

Source

fn get_value(&self, key: &str) -> Option<Arc<dyn Any + Send + Sync>>

Retrieve a type-erased value by key. Returns None on miss.

Backends that store serialized data (e.g. Redis) may return Arc<RawCacheBytes> here; get_cached handles the JSON deserialization transparently.

Source

fn insert_value(&self, key: &str, value: Arc<dyn Any + Send + Sync>)

Store a type-erased value by key.

Source

fn invalidate(&self, key: &str)

Remove a specific key.

Source

fn clear(&self)

Remove all entries.

Provided Methods§

Source

fn insert_raw_bytes(&self, _key: &str, _bytes: Vec<u8>, _ttl: Option<Duration>)

Store pre-serialized JSON bytes for backends that persist data across process boundaries (e.g. Redis). The default is a no-op; in-process backends store values via insert_value instead.

ttl carries the same time-to-live that was declared on the #[cached(ttl = "…")] attribute so backends can apply native expiry (e.g. Redis SET EX). None means no expiry.

Implementors§