Cache

Enum Cache 

Source
pub enum Cache<K, V, S = DefaultHasher, P = CacheProperties>
where K: Key, V: Value, S: HashBuilder, P: Properties,
{ Fifo(Arc<RawCache<Fifo<K, V, P>, S>>), Lru(Arc<RawCache<Lru<K, V, P>, S>>), Lfu(Arc<RawCache<Lfu<K, V, P>, S>>), S3Fifo(Arc<RawCache<S3Fifo<K, V, P>, S>>), Sieve(Arc<RawCache<Sieve<K, V, P>, S>>), }
Expand description

In-memory cache with plug-and-play algorithms.

Variants§

§

Fifo(Arc<RawCache<Fifo<K, V, P>, S>>)

In-memory FIFO cache.

§

Lru(Arc<RawCache<Lru<K, V, P>, S>>)

In-memory LRU cache.

§

Lfu(Arc<RawCache<Lfu<K, V, P>, S>>)

In-memory LFU cache.

§

S3Fifo(Arc<RawCache<S3Fifo<K, V, P>, S>>)

In-memory S3FIFO cache.

§

Sieve(Arc<RawCache<Sieve<K, V, P>, S>>)

In-memory Sieve cache.

Implementations§

Source§

impl<K, V> Cache<K, V, DefaultHasher, CacheProperties>
where K: Key, V: Value,

Source

pub fn builder(capacity: usize) -> CacheBuilder<K, V, DefaultHasher>

Create a new in-memory cache builder with capacity.

Source§

impl<K, V, S, P> Cache<K, V, S, P>
where K: Key, V: Value, S: HashBuilder, P: Properties,

Source

pub fn resize(&self, capacity: usize) -> Result<()>

Update capacity and evict overflowed entries.

Source

pub fn insert(&self, key: K, value: V) -> CacheEntry<K, V, S, P>

Insert cache entry to the in-memory cache.

Source

pub fn insert_with_properties( &self, key: K, value: V, properties: P, ) -> CacheEntry<K, V, S, P>

Insert cache entry to the in-memory cache with properties.

Source

pub fn remove<Q>(&self, key: &Q) -> Option<CacheEntry<K, V, S, P>>
where Q: Hash + Equivalent<K> + ?Sized,

Remove a cached entry with the given key from the in-memory cache.

Source

pub fn get<Q>(&self, key: &Q) -> Option<CacheEntry<K, V, S, P>>
where Q: Hash + Equivalent<K> + ?Sized,

Get cached entry with the given key from the in-memory cache.

Source

pub fn contains<Q>(&self, key: &Q) -> bool
where Q: Hash + Equivalent<K> + ?Sized,

Check if the in-memory cache contains a cached entry with the given key.

Source

pub fn touch<Q>(&self, key: &Q) -> bool
where Q: Hash + Equivalent<K> + ?Sized,

Access the cached entry with the given key but don’t return.

Note: This method can be used to update the cache eviction information and order based on the algorithm.

Source

pub fn clear(&self)

Clear the in-memory cache.

Source

pub fn capacity(&self) -> usize

Get the capacity of the in-memory cache.

Source

pub fn usage(&self) -> usize

Get the usage of the in-memory cache.

Source

pub fn hash<Q>(&self, key: &Q) -> u64
where Q: Hash + ?Sized,

Hash the given key with the hash builder of the cache.

Source

pub fn hash_builder(&self) -> &Arc<S>

Get the hash builder of the in-memory cache.

Source

pub fn shards(&self) -> usize

Get the shards of the in-memory cache.

Source

pub fn evict_all(&self)

Evict all entries from the in-memory cache.

Instead of Cache::clear, Cache::evict_all will send the evicted pipe to the pipe. It is useful when the cache is used as a hybrid cache.

Source

pub async fn flush(&self)

Evict all entries in the cache and offload them into the disk cache via the pipe if needed.

This function obeys the io throttler of the disk cache and make sure all entries will be offloaded. Therefore, this function is asynchronous.

Source§

impl<K, V, S, P> Cache<K, V, S, P>
where K: Key + Clone, V: Value, S: HashBuilder, P: Properties,

Source

pub fn fetch<F, FU, ER>(&self, key: K, fetch: F) -> Fetch<K, V, ER, S, P>
where F: FnOnce() -> FU, FU: Future<Output = Result<V, ER>> + Send + 'static, ER: Send + 'static + Debug,

Get the cached entry with the given key from the in-memory cache.

Use fetch to fetch the cache value from the remote storage on cache miss.

The concurrent fetch requests will be deduplicated.

Source

pub fn fetch_with_properties<F, FU, ER>( &self, key: K, properties: P, fetch: F, ) -> Fetch<K, V, ER, S, P>
where F: FnOnce() -> FU, FU: Future<Output = Result<V, ER>> + Send + 'static, ER: Send + 'static + Debug,

Get the cached entry with the given key and properties from the in-memory cache.

Use fetch to fetch the cache value from the remote storage on cache miss.

The concurrent fetch requests will be deduplicated.

Trait Implementations§

Source§

impl<K, V, S, P> Clone for Cache<K, V, S, P>
where K: Key, V: Value, S: HashBuilder, P: Properties,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K, V, S, P> Debug for Cache<K, V, S, P>
where K: Key, V: Value, S: HashBuilder, P: Properties,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<K, V, S, P> Freeze for Cache<K, V, S, P>

§

impl<K, V, S = BuildHasherDefault<Hasher>, P = CacheProperties> !RefUnwindSafe for Cache<K, V, S, P>

§

impl<K, V, S, P> Send for Cache<K, V, S, P>

§

impl<K, V, S, P> Sync for Cache<K, V, S, P>

§

impl<K, V, S, P> Unpin for Cache<K, V, S, P>

§

impl<K, V, S = BuildHasherDefault<Hasher>, P = CacheProperties> !UnwindSafe for Cache<K, V, S, P>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Scope for T

Source§

fn with<F, R>(self, f: F) -> R
where Self: Sized, F: FnOnce(Self) -> R,

Scoped with ownership.
Source§

fn with_ref<F, R>(&self, f: F) -> R
where F: FnOnce(&Self) -> R,

Scoped with reference.
Source§

fn with_mut<F, R>(&mut self, f: F) -> R
where F: FnOnce(&mut Self) -> R,

Scoped with mutable reference.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> Value for T
where T: Send + Sync + 'static,