pub struct WriteBehindCache<S: BackingStore> { /* private fields */ }Expand description
Write-behind cache that defers writes to a BackingStore until
flush is called.
§Dirty tracking
Every put marks the entry as dirty. flush iterates all dirty
entries, writes them to the backing store, and clears the dirty flag.
flush_if_needed only flushes when the dirty count exceeds a threshold.
§Eviction
Entries are evicted in insertion order (FIFO) when the cache exceeds
capacity. Dirty entries are flushed before eviction so data is
never silently lost.
Implementations§
Source§impl<S: BackingStore> WriteBehindCache<S>
impl<S: BackingStore> WriteBehindCache<S>
Sourcepub fn new(capacity: usize, store: S) -> Self
pub fn new(capacity: usize, store: S) -> Self
Create a new write-behind cache with the given capacity and backing store.
Sourcepub fn put(
&mut self,
key: S::Key,
value: S::Value,
) -> Result<(), WriteBehindError<S::Error>>
pub fn put( &mut self, key: S::Key, value: S::Value, ) -> Result<(), WriteBehindError<S::Error>>
Insert or update (key, value). The entry is marked dirty.
If the cache is at capacity, the oldest entry is evicted (and flushed if dirty).
Sourcepub fn get(
&mut self,
key: &S::Key,
) -> Result<Option<&S::Value>, WriteBehindError<S::Error>>
pub fn get( &mut self, key: &S::Key, ) -> Result<Option<&S::Value>, WriteBehindError<S::Error>>
Look up key. On a cache miss, attempts to load from the backing
store (read-through).
Sourcepub fn delete(
&mut self,
key: &S::Key,
) -> Result<bool, WriteBehindError<S::Error>>
pub fn delete( &mut self, key: &S::Key, ) -> Result<bool, WriteBehindError<S::Error>>
Remove key from the cache and the backing store.
Sourcepub fn flush(&mut self) -> Result<usize, WriteBehindError<S::Error>>
pub fn flush(&mut self) -> Result<usize, WriteBehindError<S::Error>>
Flush all dirty entries to the backing store.
Returns the number of entries flushed.
Sourcepub fn flush_if_needed(
&mut self,
threshold: usize,
) -> Result<usize, WriteBehindError<S::Error>>
pub fn flush_if_needed( &mut self, threshold: usize, ) -> Result<usize, WriteBehindError<S::Error>>
Flush only if the number of dirty entries exceeds threshold.
Sourcepub fn dirty_count(&self) -> usize
pub fn dirty_count(&self) -> usize
Return the number of dirty entries.
Sourcepub fn stats(&self) -> WriteBehindStats
pub fn stats(&self) -> WriteBehindStats
Return a statistics snapshot.
Sourcepub fn contains(&self, key: &S::Key) -> bool
pub fn contains(&self, key: &S::Key) -> bool
Returns true if the cache contains an entry for key.
Sourcepub fn flush_older_than(
&mut self,
max_age: Duration,
) -> Result<usize, WriteBehindError<S::Error>>
pub fn flush_older_than( &mut self, max_age: Duration, ) -> Result<usize, WriteBehindError<S::Error>>
Flush only entries whose dirty age exceeds max_age.
“Dirty age” is the time since the entry was last modified. This allows the caller to implement time-based flush policies (e.g. flush all entries older than 5 seconds).
Returns the number of entries flushed.
Sourcepub fn dirty_keys(&self) -> Vec<S::Key>
pub fn dirty_keys(&self) -> Vec<S::Key>
Return a list of all dirty keys.
Sourcepub fn mark_clean(&mut self, key: &S::Key) -> bool
pub fn mark_clean(&mut self, key: &S::Key) -> bool
Mark an entry as clean without writing to the backing store.
Useful when the caller knows the backing store is already up to date
(e.g. after an external write). Returns true if the entry was
dirty and is now clean.