pub(crate) struct AsyncLruCache<K: Clone + Copy + Debug + PartialEq + Eq + Hash + Send + Sync, V: Send + Sync, B: AsyncLruCacheBackend<Key = K, Value = V>>(Arc<AsyncLruCacheInner<K, V, B>>);Expand description
Least-recently-used cache with async access.
Keeps the least recently used entries up to a limited count. Accessing and flushing is async-aware.
K is the key used to uniquely identify cache entries, V is the cached data.
Tuple Fields§
§0: Arc<AsyncLruCacheInner<K, V, B>>Implementations§
Source§impl<K: Clone + Copy + Debug + PartialEq + Eq + Hash + Send + Sync, V: Send + Sync, B: AsyncLruCacheBackend<Key = K, Value = V>> AsyncLruCache<K, V, B>
impl<K: Clone + Copy + Debug + PartialEq + Eq + Hash + Send + Sync, V: Send + Sync, B: AsyncLruCacheBackend<Key = K, Value = V>> AsyncLruCache<K, V, B>
Sourcepub fn new(backend: B, size: usize) -> Self
pub fn new(backend: B, size: usize) -> Self
Create a new cache.
size is the maximum number of entries to keep in the cache.
Sourcepub async fn get_or_insert(
&self,
key: K,
may_flush: bool,
) -> Result<Option<Arc<V>>>
pub async fn get_or_insert( &self, key: K, may_flush: bool, ) -> Result<Option<Arc<V>>>
Retrieve an entry from the cache.
If there is no entry yet, load it via the backend.
If there is no more room in the cache for a new entry and may_flush is true, flush out
the oldest entry via flush() to make space.
Ok(None) is returned if and only if there is no more room in the cache and may_flush is
false.
Sourcepub async fn insert(
&self,
key: K,
value: Arc<V>,
may_flush: bool,
) -> Result<bool>
pub async fn insert( &self, key: K, value: Arc<V>, may_flush: bool, ) -> Result<bool>
Force-insert the given object into the cache.
If there is an existing object under that key and may_flush is true, it is flushed first.
If there is no existing object yet, i.e. a new entry must be created, but there is no more
room in the cache for this new entry, and may_flush is true, the oldest entry is flushed
out first to make space.
On success, Ok(true) is returned. Ok(false) is returned if and only if may_flush was
false, but an older cache entry would need to be flushed.
Sourcepub async fn flush(&self) -> Result<()>
pub async fn flush(&self) -> Result<()>
Flush all cache entries.
Those entries are not evicted, but remain in the cache.
Sourcepub async unsafe fn invalidate(&self) -> Result<()>
pub async unsafe fn invalidate(&self) -> Result<()>
Evict all cache entries.
Evicts all cache entries without flushing them.
§Safety
Depending on the nature of the cache, this operation may be unsafe. Perform at your own risk.