Skip to main content

CacheTier

Trait CacheTier 

Source
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: Returns Err(SizeError::unsupported()) (not all tiers track size)
  • is_empty: Delegates to len, returning Ok(true) when the reported length is 0 and otherwise propagating any SizeError

Required Methods§

Source

fn get( &self, key: &K, ) -> impl Future<Output = Result<Option<CacheEntry<V>>, Error>> + Send

Gets a value, returning an error if the operation fails.

Source

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.

Source

fn invalidate(&self, key: &K) -> impl Future<Output = Result<(), Error>> + Send

Invalidates a value, returning an error if the operation fails.

Source

fn clear(&self) -> impl Future<Output = Result<(), Error>> + Send

Clears all entries, returning an error if the operation fails.

Provided Methods§

Source

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.

Source

fn is_empty(&self) -> impl Future<Output = Result<bool, SizeError>> + Send

Returns Ok(true) if the cache appears to contain no entries.

Default implementation delegates to len.

§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".

Implementors§

Source§

impl<K, V, H> CacheTier<K, V> for InMemoryCache<K, V, H>
where K: Clone + Hash + Eq + Send + Sync + 'static, V: Clone + Send + Sync + 'static, H: BuildHasher + Clone + Send + Sync + 'static,

Source§

impl<K, V, S> CacheTier<K, V> for ServiceAdapter<K, V, S>
where K: Clone + Eq + Hash + Send + Sync + 'static, V: Clone + Send + Sync + 'static, S: Service<CacheOperation<K, V>, Out = Result<CacheResponse<V>, Error>> + Send + Sync,

Source§

impl<K, V> CacheTier<K, V> for DynamicCache<K, V>
where K: Send + Sync, V: Send,

Source§

impl<K, V> CacheTier<K, V> for MockCache<K, V>
where K: Clone + Eq + Hash + Send + Sync, V: Clone + Send + Sync,