Struct Cache

Source
pub struct Cache<K, V, S = DefaultHashBuilder> {
    pub on_evict: Option<fn(u64, u64, &V, i64)>,
    pub numb_counters: i64,
    pub buffer_items: usize,
    pub max_cost: i64,
    /* private fields */
}
Expand description

Cache is a thread-safe implementation of a hashmap with a TinyLFU admission policy and a Sampled LFU eviction policy. You can use the same Cache instance from as many goroutines as you want.

Fields§

§on_evict: Option<fn(u64, u64, &V, i64)>§numb_counters: i64§buffer_items: usize§max_cost: i64

Implementations§

Source§

impl<K, V> Cache<K, V, DefaultHashBuilder>

Source

pub fn new() -> Self

Source

pub fn with_config(c: Config<K, V>) -> Self

Source§

impl<K, V, S> Cache<K, V, S>

Source

pub fn with_hasher(hash_builder: S, c: Config<K, V>) -> Self

Source

pub fn guard(&self) -> Guard<'_>

Pin a Guard for use with this map.

Keep in mind that for as long as you hold onto this Guard, you are preventing the collection of garbage generated by the map.

Source§

impl<V, K, S> Cache<K, V, S>
where K: Hash + Ord, S: BuildHasher,

Source

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

Source

pub fn get<'g, Q: ?Sized + Hash + 'static>( &'g self, key: &Q, guard: &'g Guard<'_>, ) -> Option<&'g V>

Get returns the value (if any) and a boolean representing whether the value was found or not. The value can be nil and the boolean can be true at the same time.

Source§

impl<V, K, S> Cache<K, V, S>
where K: Sync + Send + Clone + Hash + Ord + 'static, V: Sync + Send, S: BuildHasher,

Source

pub fn set<'g>( &'g self, key: K, value: V, cost: i64, guard: &'g Guard<'_>, ) -> bool

Set attempts to add the key-value item to the cache. If it returns false, then the Set was dropped and the key-value item isn’t added to the cache. If it returns true, there’s still a chance it could be dropped by the policy if its determined that the key-value item isn’t worth keeping, but otherwise the item will be added and other items will be evicted in order to make room.

To dynamically evaluate the items cost using the Config.Coster function, set the cost parameter to 0 and Coster will be ran when needed in order to find the items true cost.

Source

pub fn set_with_ttl<'g>( &'g self, key: K, value: V, cost: i64, ttl: Duration, guard: &'g Guard<'_>, ) -> bool

SetWithTTL works like Set but adds a key-value pair to the cache that will expire after the specified TTL (time to live) has passed. A zero value means the value never expires, which is identical to calling Set. A negative value is a no-op and the value is discarded.

Source

pub fn del<'g, Q: ?Sized + Hash + 'static>( &'g self, key: &Q, guard: &'g Guard<'_>, )

Del deletes the key-value item from the cache if it exists.

Source

pub fn clear<'g>(&'g self, guard: &'g Guard<'_>)

Clear empties the hashmap and zeroes all policy counters. Note that this is not an atomic operation (but that shouldn’t be a problem as it’s assumed that Set/Get calls won’t be occurring until after this).

Source

pub fn process_items<'g>( &'g self, node: Node<V>, item: Item<V>, cost: i64, guard: &'g Guard<'_>, )

Source

pub fn clean_up<'g>(&'g self, guard: &'g Guard<'_>)

Trait Implementations§

Source§

impl<K, V, S> Clone for Cache<K, V, S>
where K: Sync + Send + Clone + Hash + Ord, V: Sync + Send + Clone, S: BuildHasher + Clone,

Source§

fn clone(&self) -> Cache<K, V, S>

Returns a copy 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> Debug for Cache<K, V, S>

Source§

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

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

impl<K, V, S> Default for Cache<K, V, S>
where S: Default,

Source§

fn default() -> Self

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

impl<K, V, S> Drop for Cache<K, V, S>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<K, V, S = RandomState> !Freeze for Cache<K, V, S>

§

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

§

impl<K, V, S> Send for Cache<K, V, S>
where S: Send, K: Send,

§

impl<K, V, S> Sync for Cache<K, V, S>
where S: Sync, K: Sync,

§

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

§

impl<K, V, S = RandomState> !UnwindSafe for Cache<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> 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.
Source§

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

Source§

fn vzip(self) -> V