Cache

Trait Cache 

Source
pub trait Cache: Send + Sync {
    // Required methods
    fn get<T: DeserializeOwned + Send>(
        &self,
        key: &str,
    ) -> Pin<Box<dyn Future<Output = Option<T>> + Send + '_>>;
    fn set<T: Serialize + Send + Sync>(
        &self,
        key: &str,
        value: &T,
        ttl: Option<Duration>,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + '_>>;
    fn delete(
        &self,
        key: &str,
    ) -> Pin<Box<dyn Future<Output = bool> + Send + '_>>;
    fn exists(
        &self,
        key: &str,
    ) -> Pin<Box<dyn Future<Output = bool> + Send + '_>>;
    fn clear(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>>;

    // Provided methods
    fn get_many<T: DeserializeOwned + Send>(
        &self,
        keys: &[&str],
    ) -> Pin<Box<dyn Future<Output = Vec<Option<T>>> + Send + '_>> { ... }
    fn set_many<'a, T: Serialize + Send + Sync + 'a>(
        &'a self,
        entries: Vec<(String, T)>,
        ttl: Option<Duration>,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> { ... }
    fn delete_many(
        &self,
        keys: &[&str],
    ) -> Pin<Box<dyn Future<Output = usize> + Send + '_>> { ... }
    fn len(&self) -> Pin<Box<dyn Future<Output = Option<usize>> + Send + '_>> { ... }
    fn is_empty(&self) -> Pin<Box<dyn Future<Output = bool> + Send + '_>> { ... }
}
Expand description

A cache backend that can store and retrieve values

This trait defines the core operations for any cache implementation. All operations are async to support both local and remote cache backends.

Required Methods§

Source

fn get<T: DeserializeOwned + Send>( &self, key: &str, ) -> Pin<Box<dyn Future<Output = Option<T>> + Send + '_>>

Get a value from the cache

Returns None if the key doesn’t exist or has expired.

Source

fn set<T: Serialize + Send + Sync>( &self, key: &str, value: &T, ttl: Option<Duration>, ) -> Pin<Box<dyn Future<Output = ()> + Send + '_>>

Set a value in the cache with an optional TTL

If ttl is None, the cache’s default TTL will be used (if any), otherwise the entry won’t expire.

Source

fn delete(&self, key: &str) -> Pin<Box<dyn Future<Output = bool> + Send + '_>>

Delete a value from the cache

Returns true if the key existed and was deleted.

Source

fn exists(&self, key: &str) -> Pin<Box<dyn Future<Output = bool> + Send + '_>>

Check if a key exists in the cache

Source

fn clear(&self) -> Pin<Box<dyn Future<Output = ()> + Send + '_>>

Clear all entries from the cache

Provided Methods§

Source

fn get_many<T: DeserializeOwned + Send>( &self, keys: &[&str], ) -> Pin<Box<dyn Future<Output = Vec<Option<T>>> + Send + '_>>

Get multiple values from the cache

Returns a vector of optional values in the same order as the keys.

Source

fn set_many<'a, T: Serialize + Send + Sync + 'a>( &'a self, entries: Vec<(String, T)>, ttl: Option<Duration>, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>

Set multiple values in the cache

Default implementation calls set for each entry sequentially. Backends may override this with a more efficient batch operation.

Source

fn delete_many( &self, keys: &[&str], ) -> Pin<Box<dyn Future<Output = usize> + Send + '_>>

Delete multiple keys from the cache

Returns the number of keys that were deleted.

Source

fn len(&self) -> Pin<Box<dyn Future<Output = Option<usize>> + Send + '_>>

Get the number of entries in the cache (if supported)

Returns None if the cache doesn’t support this operation.

Source

fn is_empty(&self) -> Pin<Box<dyn Future<Output = bool> + Send + '_>>

Check if the cache is empty

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§