Skip to main content

CacheAccessor

Trait CacheAccessor 

Source
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:

  1. Call get(key) to check for cached value
  2. If Some(cached), validate with cached.is_valid_for(&current_meta)
  3. If invalid or missing, compute new value and call put(key, new_value)

Required Methods§

Source

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.

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.

Provided Methods§

Source

fn is_empty(&self) -> bool

Check if the cache collection is empty.

Implementors§