Struct AsyncLruCacheInner

Source
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>>>,
    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.

§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>

Source

async fn ensure_free_entry( &self, map: &mut RwLockWriteGuard<'_, HashMap<K, AsyncLruCacheEntry<V>>>, may_evict: bool, ) -> Result<bool>

Ensure there is at least one free entry in the cache.

If there are free entries, return Ok(true) immediately.

If there are no free entries and may_evict is true, evict the least-recently-used entry by flushing it via backend.flush(), then return Ok(true) on success.

If there are no free entries and may_evict is false, return Ok(false).

Note that this function holds the write lock for its entire lifetime, so backend.flush() must not call back into this cache directly or indirectly. Cross-cache flush ordering must be handled externally (e.g. by qcow2’s MetadataCaches).

Source

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.

Users must not create weak references to the returned Arc.

Source

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.

Source

async fn flush(&self) -> Result<()>

Flush all cache entries.

Those entries are not evicted, but remain in the cache.

Source

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.

Auto Trait Implementations§

§

impl<Key, Value, IoBackend> !Freeze for AsyncLruCacheInner<Key, Value, IoBackend>

§

impl<Key, Value, IoBackend> !RefUnwindSafe for AsyncLruCacheInner<Key, Value, IoBackend>

§

impl<Key, Value, IoBackend> Send for AsyncLruCacheInner<Key, Value, IoBackend>

§

impl<Key, Value, IoBackend> Sync for AsyncLruCacheInner<Key, Value, IoBackend>

§

impl<Key, Value, IoBackend> Unpin for AsyncLruCacheInner<Key, Value, IoBackend>
where IoBackend: Unpin, Key: Unpin,

§

impl<Key, Value, IoBackend> UnwindSafe for AsyncLruCacheInner<Key, Value, IoBackend>
where IoBackend: UnwindSafe, Key: UnwindSafe, Value: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more