Skip to main content

Cache

Trait Cache 

Source
pub trait Cache<K: CacheKey, V: CacheValue>: Send + Sync {
Show 14 methods // Required methods fn get(&self, key: &K) -> Option<V>; fn put(&self, key: &K, value: V) -> Option<V>; fn remove(&self, k: &K) -> Option<V>; fn contains_key(&self, k: &K) -> bool; fn len(&self) -> usize; fn clear(&self); fn name(&self) -> String; fn cache_limit(&self) -> usize; fn update_cache_limit(&self, limit: usize); fn cache_ttl(&self) -> Option<Duration>; fn update_cache_ttl(&self, _ttl: Option<Duration>); fn drop_table_entries(&self, table_ref: &TableReference) -> Result<()>; fn list_entries(&self) -> HashMap<K, CacheEntryInfo<V>>; // Provided method fn is_empty(&self) -> bool { ... }
}
Expand description

Base trait for cache implementations with common operations.

This trait provides the fundamental cache operations (get, put, remove, etc.) that all cache types share.

§Thread Safety

Implementations must handle their own locking via internal mutability, as methods do not take mutable references and may be accessed by multiple concurrent queries.

Required Methods§

Source

fn get(&self, key: &K) -> Option<V>

Get a cached entry if it exists.

Source

fn put(&self, key: &K, value: V) -> Option<V>

Store a value in the cache.

Returns the previous value if one existed.

Source

fn remove(&self, k: &K) -> Option<V>

Remove an entry from the cache, returning the value if it existed.

Source

fn contains_key(&self, k: &K) -> bool

Check if the cache contains a specific key.

Source

fn len(&self) -> usize

Fetch the total number of cache entries.

Source

fn clear(&self)

Remove all entries from the cache.

Source

fn name(&self) -> String

Return the cache name.

Source

fn cache_limit(&self) -> usize

Current memory budget, in bytes.

Source

fn update_cache_limit(&self, limit: usize)

Change the memory budget in bytes.

Source

fn cache_ttl(&self) -> Option<Duration>

Time-to-live applied to newly inserted entries, or None if entries never expire on their own.

Source

fn update_cache_ttl(&self, _ttl: Option<Duration>)

Change the TTL applied to subsequent inserts.

Source

fn drop_table_entries(&self, table_ref: &TableReference) -> Result<()>

Invalidate every entry associated with table_ref.

Source

fn list_entries(&self) -> HashMap<K, CacheEntryInfo<V>>

Snapshot of all current entries with per-entry metadata (size, hits, expiration) for diagnostics and observability.

Provided Methods§

Source

fn is_empty(&self) -> bool

Check if the cache collection is empty.

Trait Implementations§

Source§

impl<K: CacheKey, V: CacheValue> Debug for dyn Cache<K, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<K: CacheKey, V: CacheValue> Cache<K, V> for DefaultCache<K, V>