Skip to main content

Cache

Trait Cache 

Source
pub trait Cache<K: Eq + Hash, V: Clone>: Send + Sync {
    // Required methods
    fn get(&self, key: &K) -> Option<V>;
    fn insert(&self, key: K, value: V) -> Option<V>;
    fn invalidate(&self, key: &K);
    fn clear(&self);
    fn size(&self) -> u64;
}
Expand description

A generic key-value cache with thread-safe operations.

Implementations must be Send + Sync so they can be shared across async tasks and stored in an Arc.

§Examples

use vlorql_core::cache::{Cache, NoopCache};

let cache = NoopCache::<u64, String>::new();
assert!(cache.get(&42).is_none());
cache.insert(42, "hello".to_owned());
assert_eq!(cache.size(), 0); // NoopCache never stores anything

Required Methods§

Source

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

Returns the cached value for key, or None on a miss.

Source

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

Inserts a value into the cache.

Returns the previous value associated with the key, if any.

Source

fn invalidate(&self, key: &K)

Removes the entry for key from the cache.

Source

fn clear(&self)

Removes all entries from the cache.

Source

fn size(&self) -> u64

Returns the number of entries currently in the cache.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§

Source§

impl<K: Eq + Hash + Send + Sync, V: Clone + Send + Sync> Cache<K, V> for NoopCache<K, V>

Source§

impl<K: Eq + Hash + Send + Sync, V: Clone + Send> Cache<K, V> for MemoryCache<K, V>