pub struct WriteBackCache<S, C> { /* private fields */ }Expand description
A cache adapter that defers writes to the backing KvStore.
Writes are buffered in the cache and marked as dirty. The store is not updated until:
WriteBackCache::flushis called explicitly, or- The inner cache evicts a dirty entry (to avoid silent data loss).
§Eviction of dirty entries
The Cache trait has no eviction callback. This adapter uses a
cooperative model: before calling put on the inner cache, it checks
whether the cache is full and peeks at the current LRU entry. If the
entry to be evicted is dirty it is flushed first. This is a best-effort
approach — the adapter introspects the cache state before insertion; it
does not hook into the cache’s internal eviction path.
§Type parameters
S: aKvStoreimplementor.C: aCache<Vec<u8>, Vec<u8>>implementor.
Implementations§
Source§impl<S, C> WriteBackCache<S, C>
impl<S, C> WriteBackCache<S, C>
Sourcepub fn new(store: S, cache: C) -> Self
pub fn new(store: S, cache: C) -> Self
Create a new write-back cache adapter with an empty dirty set.
Sourcepub fn dirty_count(&self) -> usize
pub fn dirty_count(&self) -> usize
Return the number of keys that have unflushed writes.
Sourcepub fn get(&mut self, key: &[u8]) -> Result<Option<Vec<u8>>, StoreError>
pub fn get(&mut self, key: &[u8]) -> Result<Option<Vec<u8>>, StoreError>
Look up key: cache first, then store on miss.
Store hits are not inserted back into the cache (read-around policy)
to avoid polluting the cache with cold data that has already been
committed. Callers that want read-population can call put after get.
Sourcepub fn put(&mut self, key: Vec<u8>, value: Vec<u8>) -> Result<(), StoreError>
pub fn put(&mut self, key: Vec<u8>, value: Vec<u8>) -> Result<(), StoreError>
Insert key → value into the cache and mark dirty.
The store is not updated immediately.
Sourcepub fn remove(&mut self, key: &[u8]) -> Result<(), StoreError>
pub fn remove(&mut self, key: &[u8]) -> Result<(), StoreError>
Remove key from the cache and dirty set, and delete from the store.
Sourcepub fn flush(&mut self) -> Result<(), StoreError>
pub fn flush(&mut self) -> Result<(), StoreError>
Flush all dirty keys to the store and clear the dirty set.
For each dirty key the value is read from the cache. If the key is no longer in the cache (it was evicted and presumably already flushed via the pre-eviction hook), it is skipped.