pub struct WriteThroughCache<K, V, S>{ /* private fields */ }Expand description
Write-through cache that synchronously persists every write.
§Type parameters
K– key type; must implementEq + Hash + Clone.V– value type; must implementClone.S– backing store; must implementBackingStore<K, V>.
Implementations§
Source§impl<K, V, S> WriteThroughCache<K, V, S>
impl<K, V, S> WriteThroughCache<K, V, S>
Sourcepub fn new(capacity: usize, store: S) -> Self
pub fn new(capacity: usize, store: S) -> Self
Create a new WriteThroughCache with the given capacity and backing
store.
§Panics
Panics if capacity == 0.
Sourcepub fn put(&mut self, key: K, value: V) -> Result<(), StoreError>
pub fn put(&mut self, key: K, value: V) -> Result<(), StoreError>
Write (key, value) to both the in-process cache and the backing store.
If the write to the backing store fails, the cache is not updated and an error is returned — ensuring both are consistent.
When the cache has reached capacity, the oldest entry is evicted
(from the cache only; the backing store is unaffected).
Sourcepub fn get(&mut self, key: &K) -> Option<&V>
pub fn get(&mut self, key: &K) -> Option<&V>
Retrieve the value for key.
Returns a reference to the cached value on a cache hit. On a cache miss, falls back to the backing store; if the store has the value it is inserted into the cache (write-allocate) and a reference is returned.
Returns None only when neither cache nor store contains the key.
Sourcepub fn invalidate(&mut self, key: &K) -> bool
pub fn invalidate(&mut self, key: &K) -> bool
Invalidate key from both the cache and the backing store.
Returns true if the key was present in either location.
Sourcepub fn stats(&self) -> WriteThroughStats
pub fn stats(&self) -> WriteThroughStats
Return a statistics snapshot.
Sourcepub fn backing_store(&self) -> &S
pub fn backing_store(&self) -> &S
Shared reference to the backing store.