Skip to main content

WriteBehindCache

Struct WriteBehindCache 

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

Source

pub fn new(capacity: usize, store: S) -> Self

Create a new write-behind cache with the given capacity and backing store.

Source

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).

Source

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).

Source

pub fn delete( &mut self, key: &S::Key, ) -> Result<bool, WriteBehindError<S::Error>>

Remove key from the cache and the backing store.

Source

pub fn flush(&mut self) -> Result<usize, WriteBehindError<S::Error>>

Flush all dirty entries to the backing store.

Returns the number of entries flushed.

Source

pub fn flush_if_needed( &mut self, threshold: usize, ) -> Result<usize, WriteBehindError<S::Error>>

Flush only if the number of dirty entries exceeds threshold.

Source

pub fn dirty_count(&self) -> usize

Return the number of dirty entries.

Source

pub fn stats(&self) -> WriteBehindStats

Return a statistics snapshot.

Source

pub fn store(&self) -> &S

Return a shared reference to the backing store.

Source

pub fn store_mut(&mut self) -> &mut S

Return a mutable reference to the backing store.

Source

pub fn is_dirty(&self, key: &S::Key) -> bool

Returns true if the entry for key is dirty.

Source

pub fn contains(&self, key: &S::Key) -> bool

Returns true if the cache contains an entry for key.

Source

pub fn len(&self) -> usize

Return the number of entries in the cache.

Source

pub fn is_empty(&self) -> bool

Return true when the cache is empty.

Source

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.

Source

pub fn dirty_keys(&self) -> Vec<S::Key>

Return a list of all dirty keys.

Source

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.

Source

pub fn capacity(&self) -> usize

Return the capacity of the cache.

Auto Trait Implementations§

§

impl<S> Freeze for WriteBehindCache<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for WriteBehindCache<S>

§

impl<S> Send for WriteBehindCache<S>
where S: Send, <S as BackingStore>::Key: Send, <S as BackingStore>::Value: Send,

§

impl<S> Sync for WriteBehindCache<S>
where S: Sync, <S as BackingStore>::Key: Sync, <S as BackingStore>::Value: Sync,

§

impl<S> Unpin for WriteBehindCache<S>
where S: Unpin, <S as BackingStore>::Key: Unpin, <S as BackingStore>::Value: Unpin,

§

impl<S> UnsafeUnpin for WriteBehindCache<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for WriteBehindCache<S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.