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 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 get_shard_for_key(&self, key: &CompactCacheKey) -> usize
pub fn get_shard_for_key(&self, key: &CompactCacheKey) -> usize
Get the shard index for a given cache key
This allows callers to know which shard was affected by an operation without acquiring any locks.
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: &CompactCacheKey) -> Option<usize>
pub fn peek_weight(&self, item: &CompactCacheKey) -> Option<usize>
Peek the weight associated with a cache key without changing its LRU order.