pub struct Cache<K, V, KH = DefaultKeyBuilder, C = DefaultCoster<V>, U = DefaultUpdateValidator<V>, CB = DefaultCacheCallback<V>, S = RandomState> where
K: Hash + Eq,
V: Send + Sync + 'static, {
pub metrics: Arc<Metrics>,
/* 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 threads as you want.
Features
- Internal Mutability - Do not need to use
Arc<RwLock<Cache<...>>for concurrent code, you just needCache<...> - Sync and Async - Stretto support async by
tokioand sync bycrossbeam.- In sync, Cache starts two extra OS level threads. One is policy thread, the other is writing thread.
- In async, Cache starts two extra green threads. One is policy thread, the other is writing thread.
- Store policy Stretto only store the value, which means the cache does not store the key.
- High Hit Ratios - with our unique admission/eviction policy pairing, Ristretto’s performance is best in class.
- Eviction: SampledLFU - on par with exact LRU and better performance on Search and Database traces.
- Admission: TinyLFU - extra performance with little memory overhead (12 bits per counter).
- Fast Throughput - we use a variety of techniques for managing contention and the result is excellent throughput.
- Cost-Based Eviction - any large new item deemed valuable can evict multiple smaller items (cost could be anything).
- Fully Concurrent - you can use as many threads as you want with little throughput degradation.
- Metrics - optional performance metrics for throughput, hit ratios, and other stats.
- Simple API - just figure out your ideal
CacheBuildervalues and you’re off and running.
Fields
metrics: Arc<Metrics>the metrics for the cache
Implementations
sourceimpl<K, V, KH, C, U, CB, S> Cache<K, V, KH, C, U, CB, S> where
K: Hash + Eq,
V: Send + Sync + 'static,
KH: KeyBuilder<K>,
C: Coster<V>,
U: UpdateValidator<V>,
CB: CacheCallback<V>,
S: BuildHasher + Clone + 'static,
impl<K, V, KH, C, U, CB, S> Cache<K, V, KH, C, U, CB, S> where
K: Hash + Eq,
V: Send + Sync + 'static,
KH: KeyBuilder<K>,
C: Coster<V>,
U: UpdateValidator<V>,
CB: CacheCallback<V>,
S: BuildHasher + Clone + 'static,
sourcepub fn insert(&self, key: K, val: V, cost: i64) -> bool
This is supported on crate feature sync only.
pub fn insert(&self, key: K, val: V, cost: i64) -> bool
sync only.insert attempts to add the key-value item to the cache. If it returns false,
then the insert 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 insert_with_ttl(&self, key: K, val: V, cost: i64, ttl: Duration) -> bool
This is supported on crate feature sync only.
pub fn insert_with_ttl(&self, key: K, val: V, cost: i64, ttl: Duration) -> bool
sync only.insert_with_ttl 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 insert.
sourcepub fn insert_if_present(&self, key: K, val: V, cost: i64) -> bool
This is supported on crate feature sync only.
pub fn insert_if_present(&self, key: K, val: V, cost: i64) -> bool
sync only.insert_if_present is like insert, but only updates the value of an existing key. It
does NOT add the key to cache if it’s absent.
sourcepub fn wait(&self) -> Result<(), CacheError>
This is supported on crate feature sync only.
pub fn wait(&self) -> Result<(), CacheError>
sync only.wait until all the previous operations finished.
sourcepub fn remove(&self, k: &K)
This is supported on crate feature sync only.
pub fn remove(&self, k: &K)
sync only.remove an entry from Cache by key.
sourcepub fn close(&self) -> Result<(), CacheError>
This is supported on crate feature sync only.
pub fn close(&self) -> Result<(), CacheError>
sync only.close stops all threads and closes all channels.
sourceimpl<K: Hash + Eq, V: Send + Sync + 'static> Cache<K, V>
impl<K: Hash + Eq, V: Send + Sync + 'static> Cache<K, V>
sourcepub fn new(num_counters: usize, max_cost: i64) -> Result<Self, CacheError>
This is supported on crate feature sync only.
pub fn new(num_counters: usize, max_cost: i64) -> Result<Self, CacheError>
sync only.Returns a Cache instance with default configruations.
sourcepub fn builder(
num_counters: usize,
max_cost: i64
) -> CacheBuilder<K, V, DefaultKeyBuilder, DefaultCoster<V>, DefaultUpdateValidator<V>, DefaultCacheCallback<V>, RandomState>
This is supported on crate feature sync only.
pub fn builder(
num_counters: usize,
max_cost: i64
) -> CacheBuilder<K, V, DefaultKeyBuilder, DefaultCoster<V>, DefaultUpdateValidator<V>, DefaultCacheCallback<V>, RandomState>
sync only.Returns a Builder.
sourceimpl<K: Hash + Eq, V: Send + Sync + 'static, KH: KeyBuilder<K>> Cache<K, V, KH>
impl<K: Hash + Eq, V: Send + Sync + 'static, KH: KeyBuilder<K>> Cache<K, V, KH>
sourcepub fn new_with_key_builder(
num_counters: usize,
max_cost: i64,
index: KH
) -> Result<Self, CacheError>
This is supported on crate feature sync only.
pub fn new_with_key_builder(
num_counters: usize,
max_cost: i64,
index: KH
) -> Result<Self, CacheError>
sync only.Returns a Cache instance with default configruations.
sourceimpl<K, V, KH, C, U, CB, S> Cache<K, V, KH, C, U, CB, S> where
K: Hash + Eq,
V: Send + Sync + 'static,
KH: KeyBuilder<K>,
C: Coster<V>,
U: UpdateValidator<V>,
CB: CacheCallback<V>,
S: BuildHasher + Clone + 'static,
impl<K, V, KH, C, U, CB, S> Cache<K, V, KH, C, U, CB, S> where
K: Hash + Eq,
V: Send + Sync + 'static,
KH: KeyBuilder<K>,
C: Coster<V>,
U: UpdateValidator<V>,
CB: CacheCallback<V>,
S: BuildHasher + Clone + 'static,
sourcepub fn get(&self, key: &K) -> Option<ValueRef<'_, V, S>>
This is supported on crate feature sync only.
pub fn get(&self, key: &K) -> Option<ValueRef<'_, V, S>>
sync only.get returns a Option<ValueRef<V, SS>> (if any) representing whether the
value was found or not.
sourcepub fn get_mut(&self, key: &K) -> Option<ValueRefMut<'_, V, S>>
This is supported on crate feature sync only.
pub fn get_mut(&self, key: &K) -> Option<ValueRefMut<'_, V, S>>
sync only.get_mut returns a Option<ValueRefMut<V, SS>> (if any) representing whether the
value was found or not.
sourcepub fn get_ttl(&self, key: &K) -> Option<Duration>
This is supported on crate feature sync only.
pub fn get_ttl(&self, key: &K) -> Option<Duration>
sync only.Returns the TTL for the specified key if the item was found and is not expired.
sourcepub fn clear(&self) -> Result<(), CacheError>
This is supported on crate feature sync only.
pub fn clear(&self) -> Result<(), CacheError>
sync only.clear the Cache.
sourcepub fn max_cost(&self) -> i64
This is supported on crate feature sync only.
pub fn max_cost(&self) -> i64
sync only.max_cost returns the max cost of the cache.
sourcepub fn update_max_cost(&self, max_cost: i64)
This is supported on crate feature sync only.
pub fn update_max_cost(&self, max_cost: i64)
sync only.update_max_cost updates the maxCost of an existing cache.
Trait Implementations
sourceimpl<K, V, KH, C, U, CB, S> AsRef<Cache<K, V, KH, C, U, CB, S>> for Cache<K, V, KH, C, U, CB, S> where
K: Hash + Eq,
V: Send + Sync + 'static,
KH: KeyBuilder<K>,
C: Coster<V>,
U: UpdateValidator<V>,
CB: CacheCallback<V>,
S: BuildHasher + Clone + 'static,
This is supported on crate feature sync only.
impl<K, V, KH, C, U, CB, S> AsRef<Cache<K, V, KH, C, U, CB, S>> for Cache<K, V, KH, C, U, CB, S> where
K: Hash + Eq,
V: Send + Sync + 'static,
KH: KeyBuilder<K>,
C: Coster<V>,
U: UpdateValidator<V>,
CB: CacheCallback<V>,
S: BuildHasher + Clone + 'static,
sync only.sourceimpl<K, V, KH, C, U, CB, S> Clone for Cache<K, V, KH, C, U, CB, S> where
K: Hash + Eq,
V: Send + Sync + 'static,
KH: KeyBuilder<K>,
C: Coster<V>,
U: UpdateValidator<V>,
CB: CacheCallback<V>,
S: BuildHasher + Clone + 'static,
This is supported on crate feature sync only.
impl<K, V, KH, C, U, CB, S> Clone for Cache<K, V, KH, C, U, CB, S> where
K: Hash + Eq,
V: Send + Sync + 'static,
KH: KeyBuilder<K>,
C: Coster<V>,
U: UpdateValidator<V>,
CB: CacheCallback<V>,
S: BuildHasher + Clone + 'static,
sync only.Auto Trait Implementations
impl<K, V, KH = DefaultKeyBuilder, C = DefaultCoster<V>, U = DefaultUpdateValidator<V>, CB = DefaultCacheCallback<V>, S = RandomState> !RefUnwindSafe for Cache<K, V, KH, C, U, CB, S>
impl<K, V, KH, C, U, CB, S> Send for Cache<K, V, KH, C, U, CB, S> where
C: Send + Sync,
CB: Send + Sync,
KH: Send + Sync,
S: Clone + BuildHasher,
U: UpdateValidator<V>,
impl<K, V, KH, C, U, CB, S> Sync for Cache<K, V, KH, C, U, CB, S> where
C: Send + Sync,
CB: Send + Sync,
KH: Send + Sync,
S: Clone + BuildHasher,
U: UpdateValidator<V>,
impl<K, V, KH, C, U, CB, S> Unpin for Cache<K, V, KH, C, U, CB, S>
impl<K, V, KH = DefaultKeyBuilder, C = DefaultCoster<V>, U = DefaultUpdateValidator<V>, CB = DefaultCacheCallback<V>, S = RandomState> !UnwindSafe for Cache<K, V, KH, C, U, CB, S>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into)Uses borrowed data to replace owned data, usually by cloning. Read more