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 methods
    fn insert_raw_bytes(
        &self,
        _key: &str,
        _bytes: Vec<u8>,
        _ttl: Option<Duration>,
    ) { ... }
    fn try_acquire_fill_lock(
        &self,
        _key: &str,
        _token: &str,
        _ttl: Duration,
    ) -> FillLockStatus { ... }
    fn release_fill_lock(&self, _key: &str, _token: &str) { ... }
}
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.

Source

fn try_acquire_fill_lock( &self, _key: &str, _token: &str, _ttl: Duration, ) -> FillLockStatus

Try to acquire a cross-replica fill lock for key, used by get_or_compute_with to ensure at most one replica refills a hot key at a time.

token identifies the caller so release_fill_lock can safely release only a lock it still owns. ttl bounds how long the lock is held if the caller crashes before releasing it.

The default implementation reports FillLockStatus::Unsupported, which degrades callers to in-process-only single-flight protection — safe for backends (like the in-process Moka cache) that have no cross-replica visibility.

Source

fn release_fill_lock(&self, _key: &str, _token: &str)

Release the fill lock previously acquired with token, if this caller still owns it. The default is a no-op, matching the default try_acquire_fill_lock returning FillLockStatus::Unsupported.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§