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 */ }
This is supported on crate feature 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 need Cache<...>
  • Sync and Async - Stretto support async by tokio and sync by crossbeam.
    • 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 CacheBuilder values and you’re off and running.

Fields

metrics: Arc<Metrics>

the metrics for the cache

Implementations

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.

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.

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.

wait until the previous operations finished.

remove entry from Cache by key.

close stops all threads and closes all channels.

Returns a Cache instance with default configruations.

Returns a Builder.

Returns a Cache instance with default configruations.

get returns a Option<ValueRef<V, SS>> (if any) representing whether the value was found or not.

get_mut returns a Option<ValueRefMut<V, SS>> (if any) representing whether the value was found or not.

Returns the TTL for the specified key if the item was found and is not expired.

clear the Cache.

max_cost returns the max cost of the cache.

update_max_cost updates the maxCost of an existing cache.

Returns the number of items in the Cache

Returns true if the cache is empty

Trait Implementations

Performs the conversion.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.