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§
Sourcefn get<T: DeserializeOwned + Send>(
&self,
key: &str,
) -> Pin<Box<dyn Future<Output = Option<T>> + Send + '_>>
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.
Sourcefn set<T: Serialize + Send + Sync>(
&self,
key: &str,
value: &T,
ttl: Option<Duration>,
) -> Pin<Box<dyn Future<Output = ()> + Send + '_>>
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.
Sourcefn delete(&self, key: &str) -> Pin<Box<dyn Future<Output = bool> + Send + '_>>
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.
Provided Methods§
Sourcefn get_many<T: DeserializeOwned + Send>(
&self,
keys: &[&str],
) -> Pin<Box<dyn Future<Output = Vec<Option<T>>> + Send + '_>>
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.
Sourcefn 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 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.
Sourcefn delete_many(
&self,
keys: &[&str],
) -> Pin<Box<dyn Future<Output = usize> + Send + '_>>
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.
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.