pub trait CacheBackend:
Send
+ Sync
+ Clone
+ 'static {
// Required methods
fn get(
&self,
key: &str,
) -> impl Future<Output = Result<Option<CacheRead>, CacheError>> + Send;
fn set(
&self,
key: String,
entry: CacheEntry,
ttl: Duration,
stale_for: Duration,
) -> impl Future<Output = Result<(), CacheError>> + Send;
fn invalidate(
&self,
key: &str,
) -> impl Future<Output = Result<(), CacheError>> + Send;
// Provided methods
fn get_keys_by_tag(
&self,
_tag: &str,
) -> impl Future<Output = Result<Vec<String>, CacheError>> + Send { ... }
fn invalidate_by_tag(
&self,
tag: &str,
) -> impl Future<Output = Result<usize, CacheError>> + Send { ... }
fn invalidate_by_tags(
&self,
tags: &[String],
) -> impl Future<Output = Result<usize, CacheError>> + Send { ... }
fn list_tags(
&self,
) -> impl Future<Output = Result<Vec<String>, CacheError>> + Send { ... }
}Expand description
Storage a CacheLayer can read and write.
Every method returns impl Future<..> + Send rather than being an
async fn. The + Send is required because the cache layer boxes backend
futures into a Send future; a bare async fn in a trait does not
guarantee it at the bound site.
Implementing this trait is unaffected by that. Write plain async fn
in your impl block, with no #[async_trait] attribute:
#[derive(Clone)]
struct MyBackend;
impl CacheBackend for MyBackend {
async fn get(&self, _key: &str) -> Result<Option<CacheRead>, CacheError> {
Ok(None)
}
async fn set(
&self,
_key: String,
_entry: CacheEntry,
_ttl: Duration,
_stale_for: Duration,
) -> Result<(), CacheError> {
Ok(())
}
async fn invalidate(&self, _key: &str) -> Result<(), CacheError> {
Ok(())
}
}Required Methods§
Sourcefn get(
&self,
key: &str,
) -> impl Future<Output = Result<Option<CacheRead>, CacheError>> + Send
fn get( &self, key: &str, ) -> impl Future<Output = Result<Option<CacheRead>, CacheError>> + Send
Fetches a cached entry by key.
Returns Ok(None) when the backend does not have a value or the
entry has expired.
Sourcefn set(
&self,
key: String,
entry: CacheEntry,
ttl: Duration,
stale_for: Duration,
) -> impl Future<Output = Result<(), CacheError>> + Send
fn set( &self, key: String, entry: CacheEntry, ttl: Duration, stale_for: Duration, ) -> impl Future<Output = Result<(), CacheError>> + Send
Stores an entry with a time-to-live and additional stale window.
Sourcefn invalidate(
&self,
key: &str,
) -> impl Future<Output = Result<(), CacheError>> + Send
fn invalidate( &self, key: &str, ) -> impl Future<Output = Result<(), CacheError>> + Send
Invalidates the cache entry for key, if present.
Provided Methods§
Sourcefn get_keys_by_tag(
&self,
_tag: &str,
) -> impl Future<Output = Result<Vec<String>, CacheError>> + Send
fn get_keys_by_tag( &self, _tag: &str, ) -> impl Future<Output = Result<Vec<String>, CacheError>> + Send
Retrieves all cache keys associated with a tag.
Returns an empty vector if tags are not supported by this backend.
Sourcefn invalidate_by_tag(
&self,
tag: &str,
) -> impl Future<Output = Result<usize, CacheError>> + Send
fn invalidate_by_tag( &self, tag: &str, ) -> impl Future<Output = Result<usize, CacheError>> + Send
Invalidates all cache entries associated with a tag.
Returns the number of entries invalidated.
Invalidates all cache entries associated with multiple tags.
Returns the total number of entries invalidated (may include duplicates).
Lists all currently indexed tags.
Returns an empty vector if tags are not supported by this backend.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".