pub struct TtlCache<K, V> { /* private fields */ }Expand description
Thread-safe TTL cache with stale-while-revalidate semantics.
This cache supports:
- Automatic expiration based on TTL
- Serving stale data when fresh data is unavailable
- Thread-safe access via RwLock
§Example
use std::time::Duration;
use seer_core::cache::TtlCache;
let cache: TtlCache<String, String> = TtlCache::new(Duration::from_secs(3600));
// Insert a value
cache.insert("key".to_string(), "value".to_string());
// Get the value (returns None if expired)
if let Some(value) = cache.get(&"key".to_string()) {
println!("Got: {}", value);
}Implementations§
Source§impl<K, V> TtlCache<K, V>
impl<K, V> TtlCache<K, V>
Sourcepub fn new(default_ttl: Duration) -> Self
pub fn new(default_ttl: Duration) -> Self
Creates a new cache with the specified default TTL and default max capacity (1024).
Sourcepub fn with_max_capacity(default_ttl: Duration, max_capacity: usize) -> Self
pub fn with_max_capacity(default_ttl: Duration, max_capacity: usize) -> Self
Creates a new cache with a specified TTL and max capacity.
Sourcepub fn get(&self, key: &K) -> Option<V>
pub fn get(&self, key: &K) -> Option<V>
Gets a value from the cache if it exists and is not expired.
Returns None if the key doesn’t exist, the entry has expired,
or the lock is poisoned (with a warning logged).
Sourcepub fn get_stale(&self, key: &K) -> Option<V>
pub fn get_stale(&self, key: &K) -> Option<V>
Gets a value from the cache even if it’s expired.
This is useful for stale-while-revalidate patterns where you want to serve stale data while attempting to refresh.
Sourcepub fn needs_refresh(&self, key: &K) -> bool
pub fn needs_refresh(&self, key: &K) -> bool
Checks if a key exists and needs refresh (is stale but not expired).
Returns true if the entry exists and is past 75% of its TTL.
Sourcepub fn insert_with_ttl(&self, key: K, value: V, ttl: Duration)
pub fn insert_with_ttl(&self, key: K, value: V, ttl: Duration)
Inserts a value into the cache with a custom TTL.
If the cache exceeds max capacity, expired entries are purged first. If still over capacity, the oldest entry is evicted.
Sourcepub fn cleanup(&self)
pub fn cleanup(&self)
Removes all expired entries from the cache.
This is useful for periodic cleanup to prevent unbounded memory growth.
Auto Trait Implementations§
impl<K, V> !Freeze for TtlCache<K, V>
impl<K, V> RefUnwindSafe for TtlCache<K, V>
impl<K, V> Send for TtlCache<K, V>
impl<K, V> Sync for TtlCache<K, V>
impl<K, V> Unpin for TtlCache<K, V>
impl<K, V> UnsafeUnpin for TtlCache<K, V>
impl<K, V> UnwindSafe for TtlCache<K, V>
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request