pub struct Manager<const N: usize> { /* private fields */ }Expand description
Async LRU eviction manager.
(Nearly) drop-in replacement for super::lru::Manager that uses actor-based
shards instead of RwLock-based shards. Eviction is handled
asynchronously by dedicated eviction worker tasks.
Construct via the builder pattern: call Manager::builder with the
required arguments (weight limit, AsyncEvictionCallback, and
ShutdownWatch), then chain optional setters before calling
ManagerBuilder::build.
Implementations§
Source§impl<const N: usize> Manager<N>
impl<const N: usize> Manager<N>
Sourcepub fn builder<C>(
weight_limit: usize,
eviction_cb: Arc<C>,
shutdown: ShutdownWatch,
runtime: Handle,
) -> ManagerBuilder<C, N>where
C: AsyncEvictionCallback<CacheEntryKey>,
pub fn builder<C>(
weight_limit: usize,
eviction_cb: Arc<C>,
shutdown: ShutdownWatch,
runtime: Handle,
) -> ManagerBuilder<C, N>where
C: AsyncEvictionCallback<CacheEntryKey>,
Create a builder for constructing a Manager.
This is the only way to construct a Manager. All four
arguments are required:
weight_limit— maximum total weight before eviction.eviction_cb— callback invoked for every evicted(key, weight).shutdown—ShutdownWatchthat signals actors to stop.runtime—tokio::runtime::Handleon which internal tasks are spawned.
Sourcepub async fn shard_weight(&self, shard: usize) -> Option<usize>
pub async fn shard_weight(&self, shard: usize) -> Option<usize>
Query the exact weight of a specific shard via request/response to its
actor. Returns None if shard >= N.
Sourcepub fn shard_len(&self, shard: usize) -> usize
pub fn shard_len(&self, shard: usize) -> usize
Get the number of items in a specific shard. Lock-free, best-effort
(Relaxed atomic load).
Sourcepub fn get_shard_for_key(&self, key: &CacheEntryKey) -> usize
pub fn get_shard_for_key(&self, key: &CacheEntryKey) -> usize
Compute the shard index for a given cache entry using the same hash function used internally by the LRU.
Sourcepub async fn peek_lru(&self, shard: usize) -> Option<CacheEntryKey>
pub async fn peek_lru(&self, shard: usize) -> Option<CacheEntryKey>
Peek at the least-recently-used key in the given shard. Async —
sends a request/response message to the shard actor. Returns None
if shard >= N or the shard is empty.
Sourcepub fn peek_weight(&self, item: &CacheEntryKey) -> Option<usize>
pub fn peek_weight(&self, item: &CacheEntryKey) -> Option<usize>
Peek the weight of a cache entry without promoting it. Lock-free.
Returns None if the entry is absent.
Trait Implementations§
Source§impl<const N: usize> EvictionManager for Manager<N>
impl<const N: usize> EvictionManager for Manager<N>
Source§fn total_size(&self) -> usize
fn total_size(&self) -> usize
Total weight across all shards. Lock-free.
Source§fn total_items(&self) -> usize
fn total_items(&self) -> usize
Total item count across all shards. Lock-free.
Source§fn evicted_size(&self) -> usize
fn evicted_size(&self) -> usize
Accumulated weight of all evicted items since construction. Lock-free.
Source§fn evicted_items(&self) -> usize
fn evicted_items(&self) -> usize
Accumulated count of all evicted items since construction. Lock-free.
Source§fn admit(
&self,
item: CacheEntryKey,
size: usize,
_fresh_until: SystemTime,
) -> Vec<CacheEntryKey>
fn admit( &self, item: CacheEntryKey, size: usize, _fresh_until: SystemTime, ) -> Vec<CacheEntryKey>
Admit a cache key with the given size. Fire-and-forget.
Always returns vec![] — eviction is handled asynchronously by
the eviction workers and delivered via the AsyncEvictionCallback.
Source§fn increment_weight(
&self,
item: &CacheEntryKey,
delta: usize,
max_weight: Option<usize>,
) -> Vec<CacheEntryKey>
fn increment_weight( &self, item: &CacheEntryKey, delta: usize, max_weight: Option<usize>, ) -> Vec<CacheEntryKey>
Increment a cache key’s weight, admitting it if needed. Fire-and-forget.
Always returns vec![] — eviction is handled asynchronously by
the eviction workers and delivered via the AsyncEvictionCallback.
Source§fn remove(&self, item: CacheEntryKeyRef<'_>)
fn remove(&self, item: CacheEntryKeyRef<'_>)
Remove a cache key from the LRU. Fire-and-forget — enqueued on the shard’s unbounded channel, so the message is never dropped (it fails only if the actor is gone, i.e. during shutdown).
Source§fn access(
&self,
item: &CacheEntryKey,
size: usize,
_fresh_until: SystemTime,
) -> bool
fn access( &self, item: &CacheEntryKey, size: usize, _fresh_until: SystemTime, ) -> bool
Record an access to a cache key. If the key already exists it is
promoted to the head of the LRU; otherwise it is admitted with the
given size. Returns true if the key was already present (promoted),
false if it was newly admitted.
Source§fn peek(&self, item: &CacheEntryKey) -> bool
fn peek(&self, item: &CacheEntryKey) -> bool
Check whether a cache key exists in the LRU without promoting it. Lock-free.
Source§fn save<'life0, 'life1, 'async_trait>(
&'life0 self,
dir_path: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn save<'life0, 'life1, 'async_trait>(
&'life0 self,
dir_path: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Persist all shards sequentially to the given directory.
Each shard is snapshotted via the actor, serialized to MessagePack,
and written to {dir_path}/lru.data.{shard_index} using atomic rename.
Source§fn load<'life0, 'life1, 'async_trait>(
&'life0 self,
dir_path: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn load<'life0, 'life1, 'async_trait>(
&'life0 self,
dir_path: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Load the current manager’s shard files from the given directory.
Each file is deserialized from MessagePack, and the resulting items are inserted at the tail of the LRU.