pub struct TtlCache<K, V> { /* private fields */ }Expand description
Thread-safe TTL cache.
This cache supports:
- Automatic expiration based on TTL
- A capacity bound (expired, then oldest-inserted, entries are evicted — FIFO by insertion age; reads do not refresh an entry)
- 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 or the entry has expired.
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.
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
Mutably borrows from an owned value. Read more
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> 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> ⓘ
Converts
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> ⓘ
Converts
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