Skip to main content

Cache

Struct Cache 

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

A thread-safe in-memory LRU cache with TTL and tag-based invalidation.

Implementations§

Source§

impl<K, V> Cache<K, V>
where K: Eq + Hash + Clone,

Source

pub fn new(max_size: usize, default_ttl: Option<Duration>) -> Self

Create a new cache with the given max size and optional default TTL.

Source

pub fn set(&self, key: K, value: V)

Set a value with optional TTL and tags.

Source

pub fn set_with(&self, key: K, value: V, ttl: Option<Duration>, tags: &[&str])

Set a value with custom TTL and tags.

Source

pub fn get(&self, key: &K) -> Option<V>
where V: Clone,

Get a value from the cache. Returns None if not found or expired.

Increments the hit counter on success, or the miss counter on failure.

Source

pub fn has(&self, key: &K) -> bool

Check if a key exists and is not expired.

Source

pub fn delete(&self, key: &K) -> bool

Delete an entry by key.

Source

pub fn invalidate_by_tag(&self, tag: &str) -> usize

Invalidate all entries with the given tag. Returns count removed.

Source

pub fn clear(&self)

Remove all entries.

Source

pub fn size(&self) -> usize

Return the number of entries.

Source

pub fn is_empty(&self) -> bool

Returns true if the cache has no entries.

Source

pub fn max_size(&self) -> usize

Returns the maximum number of entries the cache can hold.

Source

pub fn keys(&self) -> Vec<K>

Returns a list of all non-expired keys in the cache.

Source

pub fn remove_expired(&self) -> usize

Remove all expired entries from the cache. Returns the number of entries removed.

Source

pub fn get_or_insert_with<F>(&self, key: K, f: F) -> V
where V: Clone, F: FnOnce() -> V,

Get a value from the cache, or insert one computed by the given closure if absent or expired.

Source

pub fn stats(&self) -> CacheStats

Return a snapshot of cache performance counters.

Source

pub fn get_many(&self, keys: &[K]) -> HashMap<K, V>
where V: Clone,

Retrieve multiple values at once. Returns a map of keys to their cached values, omitting any keys that are absent or expired.

Source

pub fn delete_where<F>(&self, predicate: F) -> usize
where F: Fn(&K, &V) -> bool,

Delete all entries for which the predicate returns true. Returns the number of entries removed.

Source

pub fn len(&self) -> usize

Return the number of entries (alias for size()).

Trait Implementations§

Source§

impl<K, V> Clone for Cache<K, V>
where K: Eq + Hash + Clone,

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> Debug for Cache<K, V>
where K: Eq + Hash + Clone,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<K, V> Default for Cache<K, V>
where K: Eq + Hash + Clone,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

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

§

impl<K, V> RefUnwindSafe for Cache<K, V>

§

impl<K, V> Send for Cache<K, V>
where K: Send + Sync, V: Send + Sync,

§

impl<K, V> Sync for Cache<K, V>
where K: Send + Sync, V: Send + Sync,

§

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

§

impl<K, V> UnsafeUnpin for Cache<K, V>

§

impl<K, V> UnwindSafe for Cache<K, V>

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