pub trait CacheAccessor<K, V>: Send + Sync {
// 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;
// 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. Specific cache traits like cache_manager::FileStatisticsCache,
cache_manager::ListFilesCache, and cache_manager::FileMetadataCache extend this
trait with their specialized methods.
§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.
§Validation Pattern
Validation metadata (e.g., file size, last modified time) should be embedded in the
value type V. The typical usage pattern is:
- Call
get(key)to check for cached value - If
Some(cached), validate withcached.is_valid_for(¤t_meta) - If invalid or missing, compute new value and call
put(key, new_value)
Required Methods§
Sourcefn get(&self, key: &K) -> Option<V>
fn get(&self, key: &K) -> Option<V>
Get a cached entry if it exists.
Returns the cached value without any validation. The caller should validate the returned value if freshness matters.
Sourcefn put(&self, key: &K, value: V) -> Option<V>
fn put(&self, key: &K, value: V) -> Option<V>
Store a value in the cache.
Returns the previous value if one existed.
Sourcefn remove(&self, k: &K) -> Option<V>
fn remove(&self, k: &K) -> Option<V>
Remove an entry from the cache, returning the value if it existed.
Sourcefn contains_key(&self, k: &K) -> bool
fn contains_key(&self, k: &K) -> bool
Check if the cache contains a specific key.