pub struct CacheConfigBuilder<Req, K> { /* private fields */ }Expand description
Builder for configuring and constructing a cache.
Implementations§
Source§impl<Req, K> CacheConfigBuilder<Req, K>
impl<Req, K> CacheConfigBuilder<Req, K>
Sourcepub fn max_size(self, size: usize) -> Self
pub fn max_size(self, size: usize) -> Self
Sets the maximum number of entries in the cache.
Default: 100
Sourcepub fn ttl(self, ttl: Duration) -> Self
pub fn ttl(self, ttl: Duration) -> Self
Sets the time-to-live for cached entries.
If set, entries will expire after the specified duration. Default: None (no expiration)
Sourcepub fn key_extractor<F>(self, f: F) -> Self
pub fn key_extractor<F>(self, f: F) -> Self
Sets the function that extracts a cache key from a request.
This function must be provided before building.
Sourcepub fn name(self, name: impl Into<String>) -> Self
pub fn name(self, name: impl Into<String>) -> Self
Sets the name of this cache instance for observability.
Default: "<unnamed>"
Sourcepub fn on_hit<F>(self, f: F) -> Self
pub fn on_hit<F>(self, f: F) -> Self
Registers a callback when a cache hit occurs.
A cache hit occurs when a requested entry is found in the cache and has not expired.
§Callback Signature
Fn() - Called with no parameters when a cache hit is detected.
§Example
use tower_resilience_cache::CacheConfig;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
#[derive(Clone, Hash, Eq, PartialEq)]
struct Request {
id: String,
}
let hit_count = Arc::new(AtomicUsize::new(0));
let counter = Arc::clone(&hit_count);
let config = CacheConfig::<Request, String>::builder()
.key_extractor(|req| req.id.clone())
.on_hit(move || {
let count = counter.fetch_add(1, Ordering::SeqCst);
println!("Cache hit #{}", count + 1);
})
.build();Sourcepub fn on_miss<F>(self, f: F) -> Self
pub fn on_miss<F>(self, f: F) -> Self
Registers a callback when a cache miss occurs.
A cache miss occurs when a requested entry is not found in the cache or has expired. The underlying service will be called to fetch the value, which will then be cached.
§Callback Signature
Fn() - Called with no parameters when a cache miss is detected.
§Example
use tower_resilience_cache::CacheConfig;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
#[derive(Clone, Hash, Eq, PartialEq)]
struct Request {
id: String,
}
let miss_count = Arc::new(AtomicUsize::new(0));
let counter = Arc::clone(&miss_count);
let config = CacheConfig::<Request, String>::builder()
.key_extractor(|req| req.id.clone())
.on_miss(move || {
let count = counter.fetch_add(1, Ordering::SeqCst);
println!("Cache miss #{} - fetching from service", count + 1);
})
.build();Sourcepub fn on_eviction<F>(self, f: F) -> Self
pub fn on_eviction<F>(self, f: F) -> Self
Registers a callback when an entry is evicted from the cache.
Eviction occurs when:
- The cache reaches its maximum size and needs to make room for new entries
- An entry expires due to TTL (time-to-live) configuration
§Callback Signature
Fn() - Called with no parameters when a cache eviction occurs.
§Example
use tower_resilience_cache::CacheConfig;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;
#[derive(Clone, Hash, Eq, PartialEq)]
struct Request {
id: String,
}
let eviction_count = Arc::new(AtomicUsize::new(0));
let counter = Arc::clone(&eviction_count);
let config = CacheConfig::<Request, String>::builder()
.key_extractor(|req| req.id.clone())
.max_size(100)
.ttl(Duration::from_secs(300))
.on_eviction(move || {
let count = counter.fetch_add(1, Ordering::SeqCst);
println!("Entry evicted (total: {})", count + 1);
})
.build();