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>
impl<K, V> Cache<K, V, DefaultHashBuilder>
pub fn new() -> Self
pub fn with_config(c: Config<K, V>) -> Self
Source§impl<V, K, S> Cache<K, V, S>
impl<V, K, S> Cache<K, V, S>
Source§impl<V, K, S> Cache<K, V, S>
impl<V, K, S> Cache<K, V, S>
Sourcepub fn set<'g>(
&'g self,
key: K,
value: V,
cost: i64,
guard: &'g Guard<'_>,
) -> bool
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.
Sourcepub fn set_with_ttl<'g>(
&'g self,
key: K,
value: V,
cost: i64,
ttl: Duration,
guard: &'g Guard<'_>,
) -> bool
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.
Sourcepub fn del<'g, Q: ?Sized + Hash + 'static>(
&'g self,
key: &Q,
guard: &'g Guard<'_>,
)
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.
Sourcepub fn clear<'g>(&'g self, guard: &'g Guard<'_>)
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).