Skip to main content

TwoLevelCache

Struct TwoLevelCache 

Source
pub struct TwoLevelCache<TKey, TValue: Clone> { /* private fields */ }
Expand description

Provides a lightweight two-level cache for storing key-value pairs, supporting fast retrieval and optional eviction handling.

The cache maintains a primary entry for the most recently added item and a secondary array for additional items, up to the specified capacity. When the cache exceeds its capacity, evicted values are passed to the eviction action. It is optimized for scenarios where a small number of items are frequently accessed.

Implementations§

Source§

impl<TKey: PartialEq + Clone + 'static, TValue: Clone> TwoLevelCache<TKey, TValue>

Source

pub fn new() -> Self

Creates a cache with the default secondary capacity (3), no eviction action and the default comparer.

Source

pub fn with_secondary_size(secondary_size: i32) -> Self

Creates a cache with the given secondary capacity.

§Panics

When secondary_size is negative (upstream throws ArgumentOutOfRangeException).

Source

pub fn with_options( secondary_size: i32, eviction_action: Option<Box<dyn Fn(&TValue) + Send>>, comparer: Option<Box<dyn Fn(&TKey, &TKey) -> bool + Send>>, ) -> Self

Creates a cache with the given secondary capacity, eviction action and key comparer (None compares with ==).

§Panics

When secondary_size is negative (upstream throws ArgumentOutOfRangeException).

Source§

impl<TKey: Clone, TValue: Clone> TwoLevelCache<TKey, TValue>

Source

pub fn try_get(&self, key: &TKey) -> Option<TValue>

Looks up the value stored for key.

Source

pub fn get_or_add( &mut self, key: TKey, factory: impl FnOnce(&TKey) -> TValue, ) -> TValue

Returns the value stored for key, creating and storing it with factory when the key is not in the cache.

Source

pub fn clear_and_dispose(&mut self)

Removes every entry, passing each value to the eviction action.

Trait Implementations§

Source§

impl<TKey: PartialEq + Clone + 'static, TValue: Clone> Default for TwoLevelCache<TKey, TValue>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<TKey, TValue> !RefUnwindSafe for TwoLevelCache<TKey, TValue>

§

impl<TKey, TValue> !Sync for TwoLevelCache<TKey, TValue>

§

impl<TKey, TValue> !UnwindSafe for TwoLevelCache<TKey, TValue>

§

impl<TKey, TValue> Freeze for TwoLevelCache<TKey, TValue>

§

impl<TKey, TValue> Send for TwoLevelCache<TKey, TValue>

§

impl<TKey, TValue> Unpin for TwoLevelCache<TKey, TValue>

§

impl<TKey, TValue> UnsafeUnpin for TwoLevelCache<TKey, TValue>

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> ClassDefaults for T
where T: ?Sized,

Source§

fn static_constructor()

The static constructor of a type: work done once per thread, after the properties of the type are registered and before its first instance exists (class handlers, affects_render, metadata overrides). 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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.