pub trait CacheTier<K, V>: Send + Sync {
// Required methods
fn get(
&self,
key: &K,
) -> impl Future<Output = Result<Option<CacheEntry<V>>, Error>> + Send;
fn insert(
&self,
key: K,
entry: CacheEntry<V>,
) -> impl Future<Output = Result<(), Error>> + Send;
fn invalidate(
&self,
key: &K,
) -> impl Future<Output = Result<(), Error>> + Send;
fn clear(&self) -> impl Future<Output = Result<(), Error>> + Send;
// Provided methods
fn len(&self) -> impl Future<Output = Result<u64, SizeError>> + Send { ... }
fn is_empty(&self) -> impl Future<Output = Result<bool, SizeError>> + Send { ... }
}Expand description
Trait for cache tier implementations.
Implement this trait to create custom cache backends. The cache system
wraps these in CacheWrapper to add telemetry and TTL support.
§Consistency
Implementations must provide read-after-write monotonicity on a single
instance: once insert(key, entry) returns Ok(()), any subsequent call to
get(key) on the same instance must never return data older than what was
written. It may return None (e.g., if the entry was evicted or invalidated)
or a newer value, but never a stale one that predates the most recent write.
This guarantee is scoped to a single instance - if the same backing store
is accessed through multiple CacheTier instances (e.g., separate processes
connected to the same Redis cluster), replication lag or network partitions
may cause one instance to observe stale data written through another. The
monotonicity guarantee only applies to reads and writes through the same
Rust object.
len and is_empty have default implementations:
len: ReturnsErr(SizeError::unsupported())(not all tiers track size)is_empty: Delegates tolen, returningOk(true)when the reported length is0and otherwise propagating anySizeError
Required Methods§
Sourcefn get(
&self,
key: &K,
) -> impl Future<Output = Result<Option<CacheEntry<V>>, Error>> + Send
fn get( &self, key: &K, ) -> impl Future<Output = Result<Option<CacheEntry<V>>, Error>> + Send
Gets a value, returning an error if the operation fails.
Sourcefn insert(
&self,
key: K,
entry: CacheEntry<V>,
) -> impl Future<Output = Result<(), Error>> + Send
fn insert( &self, key: K, entry: CacheEntry<V>, ) -> impl Future<Output = Result<(), Error>> + Send
Inserts or replaces a value, returning an error if the operation fails.
If the key already exists, the previous entry is replaced with the new one.
Provided Methods§
Sourcefn len(&self) -> impl Future<Output = Result<u64, SizeError>> + Send
fn len(&self) -> impl Future<Output = Result<u64, SizeError>> + Send
Returns an approximate count of entries, if the implementation supports it.
Returns Err(SizeError::unsupported()) for implementations that do not track size.
§Approximation
The returned count may include entries that have logically expired but have
not yet been evicted. Many implementations perform eviction lazily or on a
background schedule, so len() can temporarily over count after TTL expiry
or after invalidate / clear calls that have not yet been fully applied.
Do not use this value for exact bookkeeping or correctness decisions. It is suitable for approximate capacity monitoring, metrics, and health checks.
§Errors
Returns Err(SizeError::unsupported()) if the tier does not support size
reporting.
Returns an error with Failed kind if the
underlying storage operation fails.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".