Skip to main content

WriteBackCache

Struct WriteBackCache 

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

  1. WriteBackCache::flush is called explicitly, or
  2. 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: a KvStore implementor.
  • C: a Cache<Vec<u8>, Vec<u8>> implementor.

Implementations§

Source§

impl<S, C> WriteBackCache<S, C>
where S: KvStore, C: Cache<Vec<u8>, Vec<u8>>,

Source

pub fn new(store: S, cache: C) -> Self

Create a new write-back cache adapter with an empty dirty set.

Source

pub fn store(&self) -> &S

Borrow the inner store.

Source

pub fn cache(&self) -> &C

Borrow the inner cache.

Source

pub fn dirty_count(&self) -> usize

Return the number of keys that have unflushed writes.

Source

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.

Source

pub fn put(&mut self, key: Vec<u8>, value: Vec<u8>) -> Result<(), StoreError>

Insert keyvalue into the cache and mark dirty.

The store is not updated immediately.

Source

pub fn remove(&mut self, key: &[u8]) -> Result<(), StoreError>

Remove key from the cache and dirty set, and delete from the store.

Source

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.

Auto Trait Implementations§

§

impl<S, C> Freeze for WriteBackCache<S, C>
where S: Freeze, C: Freeze,

§

impl<S, C> RefUnwindSafe for WriteBackCache<S, C>

§

impl<S, C> Send for WriteBackCache<S, C>
where S: Send, C: Send,

§

impl<S, C> Sync for WriteBackCache<S, C>
where S: Sync, C: Sync,

§

impl<S, C> Unpin for WriteBackCache<S, C>
where S: Unpin, C: Unpin,

§

impl<S, C> UnsafeUnpin for WriteBackCache<S, C>
where S: UnsafeUnpin, C: UnsafeUnpin,

§

impl<S, C> UnwindSafe for WriteBackCache<S, C>
where S: UnwindSafe, C: UnwindSafe,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.