Struct stretto::AsyncCache
source · [−]pub struct AsyncCache<K, V, KH = DefaultKeyBuilder, C = DefaultCoster<V>, U = DefaultUpdateValidator<V>, CB = DefaultCacheCallback<V>, S = RandomState> where
K: Hash + Eq,
V: Send + Sync + 'static,
KH: KeyBuilder<K>, {
pub metrics: Arc<Metrics>,
/* private fields */
}async only.Expand description
AsyncCache is a thread-safe async implementation of a hashmap with a TinyLFU admission policy and a Sampled LFU eviction policy. You can use the same AsyncCache 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> AsyncCache<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> AsyncCache<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 async fn insert(&self, key: K, val: V, cost: i64) -> bool
pub async fn insert(&self, key: K, val: V, cost: i64) -> bool
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 async fn insert_with_ttl(
&self,
key: K,
val: V,
cost: i64,
ttl: Duration
) -> bool
pub async fn insert_with_ttl(
&self,
key: K,
val: V,
cost: i64,
ttl: Duration
) -> bool
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 async fn insert_if_present(&self, key: K, val: V, cost: i64) -> bool
pub async fn insert_if_present(&self, key: K, val: V, cost: i64) -> bool
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 async fn wait(&self) -> Result<(), CacheError>
pub async fn wait(&self) -> Result<(), CacheError>
wait until the previous operations finished.
sourcepub async fn close(&self) -> Result<(), CacheError>
pub async fn close(&self) -> Result<(), CacheError>
close stops all threads and closes all channels.
sourceimpl<K: Hash + Eq, V: Send + Sync + 'static> AsyncCache<K, V>
impl<K: Hash + Eq, V: Send + Sync + 'static> AsyncCache<K, V>
sourcepub fn new(num_counters: usize, max_cost: i64) -> Result<Self, CacheError>
pub fn new(num_counters: usize, max_cost: i64) -> Result<Self, CacheError>
Returns a Cache instance with default configruations.
sourcepub fn builder(
num_counters: usize,
max_cost: i64
) -> AsyncCacheBuilder<K, V, DefaultKeyBuilder, DefaultCoster<V>, DefaultUpdateValidator<V>, DefaultCacheCallback<V>, RandomState>
pub fn builder(
num_counters: usize,
max_cost: i64
) -> AsyncCacheBuilder<K, V, DefaultKeyBuilder, DefaultCoster<V>, DefaultUpdateValidator<V>, DefaultCacheCallback<V>, RandomState>
Returns a Builder.
sourceimpl<K: Hash + Eq, V: Send + Sync + 'static, KH: KeyBuilder<K>> AsyncCache<K, V, KH>
impl<K: Hash + Eq, V: Send + Sync + 'static, KH: KeyBuilder<K>> AsyncCache<K, V, KH>
sourcepub fn new_with_key_builder(
num_counters: usize,
max_cost: i64,
index: KH
) -> Result<Self, CacheError>
pub fn new_with_key_builder(
num_counters: usize,
max_cost: i64,
index: KH
) -> Result<Self, CacheError>
Returns a Cache instance with default configruations.
sourceimpl<K, V, KH, C, U, CB, S> AsyncCache<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> AsyncCache<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>>
pub fn get(&self, key: &K) -> Option<ValueRef<'_, V, S>>
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>>
pub fn get_mut(&self, key: &K) -> Option<ValueRefMut<'_, V, S>>
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>
pub fn get_ttl(&self, key: &K) -> Option<Duration>
Returns the TTL for the specified key if the item was found and is not expired.
sourcepub fn clear(&self) -> Result<(), CacheError>
pub fn clear(&self) -> Result<(), CacheError>
clear the Cache.
sourcepub fn update_max_cost(&self, max_cost: i64)
pub fn update_max_cost(&self, max_cost: i64)
update_max_cost updates the maxCost of an existing cache.
Trait Implementations
sourceimpl<K, V, KH, C, U, CB, S> AsRef<AsyncCache<K, V, KH, C, U, CB, S>> for AsyncCache<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> AsRef<AsyncCache<K, V, KH, C, U, CB, S>> for AsyncCache<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,
sourcefn as_ref(&self) -> &AsyncCache<K, V, KH, C, U, CB, S>
fn as_ref(&self) -> &AsyncCache<K, V, KH, C, U, CB, S>
Performs the conversion.
sourceimpl<K, V, KH, C, U, CB, S> Clone for AsyncCache<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> Clone for AsyncCache<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,
Auto Trait Implementations
impl<K, V, KH = DefaultKeyBuilder, C = DefaultCoster<V>, U = DefaultUpdateValidator<V>, CB = DefaultCacheCallback<V>, S = RandomState> !RefUnwindSafe for AsyncCache<K, V, KH, C, U, CB, S>
impl<K, V, KH, C, U, CB, S> Send for AsyncCache<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 AsyncCache<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 AsyncCache<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 AsyncCache<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