Skip to main content

Manager

Struct Manager 

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

Source

pub fn from_lru(lru: Arc<AsyncLru<CacheEntryKey, N>>) -> Self

Construct from a pre-built AsyncLru.

Source§

impl<const N: usize> Manager<N>

Source

pub fn builder<C>( weight_limit: usize, eviction_cb: Arc<C>, shutdown: ShutdownWatch, runtime: Handle, ) -> ManagerBuilder<C, N>

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).
  • shutdownShutdownWatch that signals actors to stop.
  • runtimetokio::runtime::Handle on which internal tasks are spawned.
Source

pub fn shards(&self) -> usize

Return the total number of shards (N).

Source

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.

Source

pub fn shard_len(&self, shard: usize) -> usize

Get the number of items in a specific shard. Lock-free, best-effort (Relaxed atomic load).

Source

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.

Source

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.

Source

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.

Source

pub async fn serialize_shard(&self, shard: usize) -> Result<Vec<u8>>

Serialize the contents of a single shard to MessagePack bytes.

Snapshots the shard via the actor (cloning all keys), then serializes the (key, weight) pairs as a msgpack sequence.

Trait Implementations§

Source§

impl<const N: usize> EvictionManager for Manager<N>

Source§

fn total_size(&self) -> usize

Total weight across all shards. Lock-free.

Source§

fn total_items(&self) -> usize

Total item count across all shards. Lock-free.

Source§

fn evicted_size(&self) -> usize

Accumulated weight of all evicted items since construction. Lock-free.

Source§

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>

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>

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<'_>)

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

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

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,

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,

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.

Auto Trait Implementations§

§

impl<const N: usize> Freeze for Manager<N>

§

impl<const N: usize> RefUnwindSafe for Manager<N>

§

impl<const N: usize> Send for Manager<N>

§

impl<const N: usize> Sync for Manager<N>

§

impl<const N: usize> Unpin for Manager<N>

§

impl<const N: usize> UnsafeUnpin for Manager<N>

§

impl<const N: usize> UnwindSafe for Manager<N>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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