pub struct CacheConfig { /* private fields */ }Expand description
Configuration for creating a new cache instance.
Use the builder pattern to construct configuration:
use in_memory_cache::CacheConfig;
use std::time::Duration;
let config = CacheConfig::new()
.max_capacity(10_000)
.default_ttl(Duration::from_secs(300))
.build();Implementations§
Source§impl CacheConfig
impl CacheConfig
Sourcepub fn max_capacity(self, capacity: usize) -> Self
pub fn max_capacity(self, capacity: usize) -> Self
Set the maximum capacity of the cache.
When the cache reaches this capacity, the least recently used entry will be evicted to make room for new entries.
§Arguments
capacity- Maximum number of entries. Use 0 for unlimited (not recommended).
Sourcepub fn default_ttl(self, ttl: Duration) -> Self
pub fn default_ttl(self, ttl: Duration) -> Self
Set the default TTL for entries.
Entries without an explicit TTL will use this value.
Set to Duration::ZERO to disable default TTL.
Sourcepub fn cleanup_interval(self, interval: Duration) -> Self
pub fn cleanup_interval(self, interval: Duration) -> Self
Set the interval for background cleanup of expired entries.
The background task will run at this interval to remove expired entries. This is in addition to lazy expiration (entries checked on access).
Sourcepub fn background_cleanup(self, enabled: bool) -> Self
pub fn background_cleanup(self, enabled: bool) -> Self
Enable or disable background cleanup.
When enabled, a background task periodically removes expired entries. When disabled, entries are only removed on access (lazy expiration).
Sourcepub fn build(self) -> Self
pub fn build(self) -> Self
Build the final configuration.
This method validates the configuration and returns the final config.
Sourcepub fn get_max_capacity(&self) -> Option<usize>
pub fn get_max_capacity(&self) -> Option<usize>
Get the maximum capacity, if set.
Sourcepub fn get_default_ttl(&self) -> Option<Duration>
pub fn get_default_ttl(&self) -> Option<Duration>
Get the default TTL, if set.
Trait Implementations§
Source§impl Clone for CacheConfig
impl Clone for CacheConfig
Source§fn clone(&self) -> CacheConfig
fn clone(&self) -> CacheConfig
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more