Skip to main content

CacheableKvStore

Struct CacheableKvStore 

Source
pub struct CacheableKvStore<S, C> { /* private fields */ }
Expand description

A read-through cache adapter that wraps a KvStore with a Cache.

  • get(k) — checks the cache first; on a miss, fetches from the store, populates the cache, and returns the value. The mutex lock is not held while calling the underlying store, so concurrent readers are never blocked by store I/O.
  • put(k, v) — writes to the store immediately and then invalidates the cached entry to prevent stale reads.
  • delete(k) — deletes from the store and invalidates the cached entry.
  • All other KvStore methods (transactions, snapshots, iteration, …) delegate directly to the inner store without cache involvement.

§Type parameters

  • S: a KvStore implementor.
  • C: a Cache<Vec<u8>, Vec<u8>> implementor that is also Send.

Implementations§

Source§

impl<S, C> CacheableKvStore<S, C>

Source

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

Create a new CacheableKvStore wrapping store and cache.

Trait Implementations§

Source§

impl<S, C> KvStore for CacheableKvStore<S, C>
where S: KvStore, C: Cache<Vec<u8>, Vec<u8>> + Send,

Source§

fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, StoreError>

Retrieve the value associated with key, or None if it is absent.
Source§

fn put(&self, key: &[u8], value: &[u8]) -> Result<(), StoreError>

Insert or overwrite a key-value pair.
Source§

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

Remove a key. No-op if the key is absent.
Source§

fn range<'a>( &'a self, lo: &[u8], hi: &[u8], ) -> Result<RangeIter<'a>, StoreError>

Return all key-value pairs whose keys fall within [lo, hi), in ascending key order.
Source§

fn iter<'a>(&'a self) -> Result<RangeIter<'a>, StoreError>

Iterate all key-value pairs in the store in ascending key order. Read more
Source§

fn transaction(&self) -> Result<Box<dyn KvTxn + '_>, StoreError>

Begin an explicit write transaction. Read more
Source§

fn snapshot(&self) -> Result<Box<dyn KvSnapshot + '_>, StoreError>

Capture a point-in-time read-only snapshot of the store.
Source§

fn flush(&self) -> Result<(), StoreError>

Ensure all committed data has been written to durable storage. Read more
Source§

fn get_many(&self, keys: &[&[u8]]) -> Result<Vec<Option<Vec<u8>>>, StoreError>

Retrieve values for multiple keys in a single call. Read more
Source§

fn get_ref<'a>( &'a self, key: &[u8], ) -> Result<Option<Cow<'a, [u8]>>, StoreError>

Retrieve a value as a std::borrow::Cow, avoiding a clone when the backend can return a borrowed slice. Read more
Source§

fn contains(&self, key: &[u8]) -> Result<bool, StoreError>

Return true if key is present in the store. Read more
Source§

fn range_rev<'a>( &'a self, lo: &[u8], hi: &[u8], ) -> Result<Box<dyn Iterator<Item = Result<(Vec<u8>, Vec<u8>), StoreError>> + 'a>, StoreError>

Return all key-value pairs whose keys fall within [lo, hi), in descending key order. Read more
Source§

fn prefix_scan<'a>( &'a self, prefix: &[u8], ) -> Result<Box<dyn Iterator<Item = Result<(Vec<u8>, Vec<u8>), StoreError>> + 'a>, StoreError>

Iterate all key-value pairs sharing the given prefix, in ascending key order. Read more
Source§

fn batch_write(&self, pairs: &[(&[u8], &[u8])]) -> Result<(), StoreError>

Insert multiple key-value pairs atomically in a single batch. Read more
Source§

fn batch_delete(&self, keys: &[&[u8]]) -> Result<(), StoreError>

Delete multiple keys atomically in a single batch. Read more
Source§

fn count(&self) -> Result<u64, StoreError>

Return the total number of keys in the store. Read more
Source§

fn size_on_disk(&self) -> Result<u64, StoreError>

Return the approximate byte size of the store on disk. Read more
Source§

fn keys<'a>( &'a self, ) -> Result<Box<dyn Iterator<Item = Result<Vec<u8>, StoreError>> + 'a>, StoreError>

Iterate all keys (without loading values) in ascending order. Read more
Source§

fn compare_and_swap( &self, key: &[u8], expected: Option<&[u8]>, new_value: &[u8], ) -> Result<bool, StoreError>

Atomic compare-and-swap: if the current value for key equals expected, replace it with new_value and return Ok(true). If the current value does not match expected, return Ok(false). Read more
Source§

fn put_with_ttl( &self, _key: &[u8], _value: &[u8], _ttl: Duration, ) -> Result<(), StoreError>

Insert a key-value pair with a time-to-live. After ttl has elapsed, the key is treated as absent (expired). Read more
Source§

fn expire(&self, _key: &[u8], _ttl: Duration) -> Result<(), StoreError>

Set a TTL on an existing key. The key must already exist. Read more
Source§

fn ttl(&self, _key: &[u8]) -> Result<Option<Duration>, StoreError>

Return the remaining TTL for a key. Read more
Source§

fn persist(&self, _key: &[u8]) -> Result<bool, StoreError>

Remove the TTL from a key, making it persistent. Read more
Source§

fn purge_expired(&self) -> Result<u64, StoreError>

Scan and delete all expired keys eagerly. Read more
Source§

fn compact(&self) -> Result<(), StoreError>

Trigger manual compaction on backends that support it. Read more
Source§

fn backup(&self, _path: &Path) -> Result<(), StoreError>

Create a point-in-time backup to the given path. Read more
Source§

fn restore(&self, _path: &Path) -> Result<(), StoreError>

Restore from a backup at the given path. Read more

Auto Trait Implementations§

§

impl<S, C> !Freeze for CacheableKvStore<S, C>

§

impl<S, C> RefUnwindSafe for CacheableKvStore<S, C>
where S: RefUnwindSafe,

§

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

§

impl<S, C> Sync for CacheableKvStore<S, C>
where S: Sync, C: Send,

§

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

§

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

§

impl<S, C> UnwindSafe for CacheableKvStore<S, C>
where S: 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.