Skip to main content

ConcurrentLfudaCache

Struct ConcurrentLfudaCache 

Source
pub struct ConcurrentLfudaCache<K, V, S = DefaultHashBuilder> { /* private fields */ }
Expand description

A thread-safe LFUDA cache with segmented storage for high concurrency.

Implementations§

Source§

impl<K, V> ConcurrentLfudaCache<K, V, DefaultHashBuilder>
where K: Hash + Eq + Clone + Send, V: Clone + Send,

Source

pub fn init( config: ConcurrentLfudaCacheConfig, hasher: Option<DefaultHashBuilder>, ) -> Self

Creates a new concurrent LFUDA cache from a configuration.

This is the recommended way to create a concurrent LFUDA cache.

§Arguments
  • config - The cache configuration specifying capacity, segments, etc.
  • hasher - Optional custom hash builder. If None, uses the default.
Source§

impl<K, V, S> ConcurrentLfudaCache<K, V, S>
where K: Hash + Eq + Clone + Send, V: Clone + Send, S: BuildHasher + Clone + Send,

Source

pub fn capacity(&self) -> usize

Returns the total capacity across all segments.

Source

pub fn segment_count(&self) -> usize

Returns the number of segments in the cache.

Source

pub fn len(&self) -> usize

Returns the total number of entries across all segments.

Source

pub fn is_empty(&self) -> bool

Returns true if the cache contains no entries.

Source

pub fn get<Q>(&self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: ?Sized + Hash + Eq,

Gets a value from the cache.

This clones the value to avoid holding the lock. For zero-copy access, use get_with() instead.

Source

pub fn get_with<Q, F, R>(&self, key: &Q, f: F) -> Option<R>
where K: Borrow<Q>, Q: ?Sized + Hash + Eq, F: FnOnce(&V) -> R,

Gets a value and applies a function to it while holding the lock.

This is more efficient than get() when you only need to read from the value, as it avoids cloning.

Source

pub fn put(&self, key: K, value: V) -> Option<(K, V)>

Inserts a key-value pair into the cache.

If the cache is at capacity, the entry with lowest priority (frequency + age) is evicted.

Source

pub fn put_with_size(&self, key: K, value: V, size: u64) -> Option<(K, V)>

Inserts a key-value pair with explicit size tracking.

Source

pub fn remove<Q>(&self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: ?Sized + Hash + Eq,

Removes a key from the cache, returning the value if it existed.

Source

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

Returns true if the cache contains the specified key.

Source

pub fn clear(&self)

Clears all entries from the cache.

Source

pub fn current_size(&self) -> u64

Returns the current total size of cached content across all segments.

Source

pub fn max_size(&self) -> u64

Returns the maximum content size the cache can hold across all segments.

Trait Implementations§

Source§

impl<K, V, S> CacheMetrics for ConcurrentLfudaCache<K, V, S>
where K: Hash + Eq + Clone + Send, V: Clone + Send, S: BuildHasher + Clone + Send,

Source§

fn metrics(&self) -> BTreeMap<String, f64>

Returns all metrics as key-value pairs in deterministic order Read more
Source§

fn algorithm_name(&self) -> &'static str

Algorithm name for identification Read more
Source§

impl<K, V, S> Debug for ConcurrentLfudaCache<K, V, S>
where K: Hash + Eq + Clone + Send, V: Clone + Send, S: BuildHasher + Clone + Send,

Source§

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

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

impl<K: Send, V: Send, S: Send> Send for ConcurrentLfudaCache<K, V, S>

Source§

impl<K: Send, V: Send, S: Send + Sync> Sync for ConcurrentLfudaCache<K, V, S>

Auto Trait Implementations§

§

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

§

impl<K, V, S = DefaultHashBuilder> !RefUnwindSafe for ConcurrentLfudaCache<K, V, S>

§

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

§

impl<K, V, S> UnwindSafe for ConcurrentLfudaCache<K, V, S>

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.