struct AsyncLruCacheInner<Key: Clone + Copy + Debug + PartialEq + Eq + Hash + Send + Sync, Value: Send + Sync, IoBackend: AsyncLruCacheBackend<Key = Key, Value = Value>> {
backend: IoBackend,
map: RwLock<HashMap<Key, AsyncLruCacheEntry<Value>>>,
flush_before: Mutex<Vec<Arc<dyn FlushableCache>>>,
lru_timer: AtomicUsize,
limit: usize,
}
Expand description
Least-recently-used cache with async access.
Fields§
§backend: IoBackend
I/O back-end that performs loading and flushing of cache entries.
map: RwLock<HashMap<Key, AsyncLruCacheEntry<Value>>>
Cache entries.
flush_before: Mutex<Vec<Arc<dyn FlushableCache>>>
Flush dependencies (flush these first).
lru_timer: AtomicUsize
Monotonically increasing counter to generate “timestamps”.
limit: usize
Upper limit of how many entries to cache.
Implementations§
Source§impl<K: Clone + Copy + Debug + PartialEq + Eq + Hash + Send + Sync, V: Send + Sync, B: AsyncLruCacheBackend<Key = K, Value = V>> AsyncLruCacheInner<K, V, B>
impl<K: Clone + Copy + Debug + PartialEq + Eq + Hash + Send + Sync, V: Send + Sync, B: AsyncLruCacheBackend<Key = K, Value = V>> AsyncLruCacheInner<K, V, B>
Sourceasync fn flush_dependencies(
flush_before: &mut MutexGuard<'_, Vec<Arc<dyn FlushableCache>>>,
) -> Result<()>
async fn flush_dependencies( flush_before: &mut MutexGuard<'_, Vec<Arc<dyn FlushableCache>>>, ) -> Result<()>
Flush all dependencies.
Flush all caches that must be flushed before this one. Remove all successfully flushed caches from our dependency list.
Call with a guard that should be dropped only after this cache is flushed, so that no new dependencies can enter while we are still flushing this cache.
Sourceasync fn ensure_free_entry(
&self,
map: &mut RwLockWriteGuard<'_, HashMap<K, AsyncLruCacheEntry<V>>>,
) -> Result<()>
async fn ensure_free_entry( &self, map: &mut RwLockWriteGuard<'_, HashMap<K, AsyncLruCacheEntry<V>>>, ) -> Result<()>
Ensure there is at least one free entry in the cache.
Do this by evicting (flushing) existing entries, if necessary.
Sourceasync fn get_or_insert(&self, key: K) -> Result<Arc<V>>
async fn get_or_insert(&self, key: K) -> Result<Arc<V>>
Retrieve an entry from the cache.
If there is no entry yet, run read()
to generate it. If then there are more entries in
the cache than its limit, flush out the oldest entry via flush()
.
Sourceasync fn insert(&self, key: K, value: Arc<V>) -> Result<()>
async fn insert(&self, key: K, value: Arc<V>) -> Result<()>
Force-insert the given object into the cache.
If there is an existing object under that key, it is flushed first.
Sourceasync fn flush(&self) -> Result<()>
async fn flush(&self) -> Result<()>
Flush all cache entries.
Those entries are not evicted, but remain in the cache.
Sourceasync unsafe fn invalidate(&self) -> Result<()>
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.