pub struct Manager<const N: usize>(/* private fields */);Expand description
A shared LRU cache manager designed to manage a large volume of assets.
- Space optimized in-memory LRU (see pingora_lru).
- Instead of a single giant LRU, this struct shards the assets into
Nindependent LRUs.
This allows EvictionManager::save() not to lock the entire cache manager while performing serialization.
Implementations§
Source§impl<const N: usize> Manager<N>
impl<const N: usize> Manager<N>
Sourcepub fn with_capacity(limit: usize, capacity: usize) -> Self
pub fn with_capacity(limit: usize, capacity: usize) -> Self
Create a Manager with the given size limit and estimated per shard capacity.
The capacity is for preallocating to avoid reallocation cost when the LRU grows.
Sourcepub fn with_capacity_and_watermark(
limit: usize,
capacity: usize,
watermark: Option<usize>,
) -> Self
pub fn with_capacity_and_watermark( limit: usize, capacity: usize, watermark: Option<usize>, ) -> Self
Create a Manager with an optional watermark in addition to weight limit.
When watermark is set, the underlying LRU will also evict to keep total item count
under or equal to that watermark.
Sourcepub fn weight_limit(&self) -> usize
pub fn weight_limit(&self) -> usize
Return the current total cache weight limit.
Sourcepub fn set_weight_limit(&self, limit: usize)
pub fn set_weight_limit(&self, limit: usize)
Set the total cache weight limit used by future eviction decisions.
Sourcepub fn shard_weight(&self, shard: usize) -> usize
pub fn shard_weight(&self, shard: usize) -> usize
Get the weight (total size) of a specific shard
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. Best-effort
lock-free read; see pingora_lru::Lru::shard_len for the
consistency semantics.
Sourcepub fn get_shard_for_key(&self, key: &CacheEntryKey) -> usize
pub fn get_shard_for_key(&self, key: &CacheEntryKey) -> usize
Get the shard index for a given cache entry
This allows callers to know which shard was affected by an operation without acquiring any locks.
Sourcepub fn peek_lru(&self, shard: usize) -> Option<CacheEntryKey>
pub fn peek_lru(&self, shard: usize) -> Option<CacheEntryKey>
Peek at the least-recently-used key in the given shard without evicting it.
Returns the cache entry at the LRU tail of the shard, or None if empty.
Useful for reporting the eviction frontier (the age of the next item
that would be evicted).
Sourcepub fn deserialize_shard(&self, buf: &[u8]) -> Result<()>
pub fn deserialize_shard(&self, buf: &[u8]) -> Result<()>
Deserialize a shard
Shard number is not needed because the key itself will hash to the correct shard.
Sourcepub fn peek_weight(&self, item: &CacheEntryKey) -> Option<usize>
pub fn peek_weight(&self, item: &CacheEntryKey) -> Option<usize>
Peek the weight associated with a cache entry without changing its LRU order.
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
Source§fn total_items(&self) -> usize
fn total_items(&self) -> usize
Source§fn evicted_size(&self) -> usize
fn evicted_size(&self) -> usize
Source§fn evicted_items(&self) -> usize
fn evicted_items(&self) -> usize
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>
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>
max_weight, and floored to 1. Read more