pub struct CacheBuilder { /* private fields */ }Expand description
Builder for configuring a Cache.
§Example
use priority_lfu::CacheBuilder;
let cache = CacheBuilder::new(1024 * 1024 * 512) // 512 MB
.shards(128)
.build();§Automatic Shard Scaling
By default, the cache uses up to 64 shards, but automatically scales down for smaller caches to ensure each shard has at least 4KB capacity. This prevents premature eviction due to uneven hash distribution.
- 256KB+ capacity: 64 shards (4KB+ per shard)
- 64KB capacity: 16 shards (4KB per shard)
- 4KB capacity: 1 shard (4KB per shard)
You can override this with [shards()], but the count may still be reduced
if the capacity is too small to support the requested number.
Implementations§
Source§impl CacheBuilder
impl CacheBuilder
Sourcepub fn new(max_size_bytes: usize) -> Self
pub fn new(max_size_bytes: usize) -> Self
Create a new builder with the given maximum size in bytes.
Sourcepub fn shards(self, count: usize) -> Self
pub fn shards(self, count: usize) -> Self
Set the number of shards.
More shards reduce contention but increase memory overhead. Will be rounded up to the next power of 2.
Note: The shard count may be reduced if the capacity is too small to support the requested number of shards (minimum 4KB per shard). This prevents premature eviction due to uneven hash distribution.
Default: up to 64 shards, scaled based on capacity
Trait Implementations§
Auto Trait Implementations§
impl Freeze for CacheBuilder
impl RefUnwindSafe for CacheBuilder
impl Send for CacheBuilder
impl Sync for CacheBuilder
impl Unpin for CacheBuilder
impl UnwindSafe for CacheBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more