Skip to main content

WriteThroughCache

Struct WriteThroughCache 

Source
pub struct WriteThroughCache<K, V, S>
where K: Eq + Hash + Clone, V: Clone, S: BackingStore<K, V>,
{ /* private fields */ }
Expand description

Write-through cache that synchronously persists every write.

§Type parameters

  • K – key type; must implement Eq + Hash + Clone.
  • V – value type; must implement Clone.
  • S – backing store; must implement BackingStore<K, V>.

Implementations§

Source§

impl<K, V, S> WriteThroughCache<K, V, S>
where K: Eq + Hash + Clone, V: Clone, S: BackingStore<K, V>,

Source

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

Create a new WriteThroughCache with the given capacity and backing store.

§Panics

Panics if capacity == 0.

Source

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

Source

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.

Source

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.

Source

pub fn stats(&self) -> WriteThroughStats

Return a statistics snapshot.

Source

pub fn cache_len(&self) -> usize

Number of entries currently in the in-process cache.

Source

pub fn capacity(&self) -> usize

Configured cache capacity.

Source

pub fn backing_store(&self) -> &S

Shared reference to the backing store.

Auto Trait Implementations§

§

impl<K, V, S> Freeze for WriteThroughCache<K, V, S>
where S: Freeze,

§

impl<K, V, S> RefUnwindSafe for WriteThroughCache<K, V, S>

§

impl<K, V, S> Send for WriteThroughCache<K, V, S>
where S: Send, K: Send, V: Send,

§

impl<K, V, S> Sync for WriteThroughCache<K, V, S>
where S: Sync, K: Sync, V: Sync,

§

impl<K, V, S> Unpin for WriteThroughCache<K, V, S>
where S: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, S> UnsafeUnpin for WriteThroughCache<K, V, S>
where S: UnsafeUnpin,

§

impl<K, V, S> UnwindSafe for WriteThroughCache<K, V, S>
where S: UnwindSafe, K: UnwindSafe, V: 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<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.