pub struct SimpleCacheObject<U> { /* private fields */ }Expand description
A cached value with metadata about its creation time and expiration.
This struct wraps the actual cached value along with timing information to support automatic expiration and cache management.
§Examples
use simple_cacher::*;
use std::time::Duration;
let mut cache = SimpleCacher::new(Duration::from_secs(60));
cache.insert("key".to_string(), "value".to_string());
if let Ok(entry) = cache.get(&"key".to_string()) {
println!("Value: {}", entry.value());
println!("Age: {:?}", entry.age());
println!("Expired: {}", entry.is_expired());
}Implementations§
Source§impl<U> SimpleCacheObject<U>
impl<U> SimpleCacheObject<U>
Sourcepub fn is_expired(&self) -> bool
pub fn is_expired(&self) -> bool
Returns true if this cache entry has expired based on its max age.
§Examples
use simple_cacher::*;
use std::time::Duration;
let mut cache = SimpleCacher::new(Duration::from_millis(100));
cache.insert("key".to_string(), "value".to_string());
if let Ok(entry) = cache.get(&"key".to_string()) {
// Should not be expired immediately
assert!(!entry.is_expired());
}Sourcepub fn value(&self) -> &U
pub fn value(&self) -> &U
Returns a reference to the cached value.
§Examples
use simple_cacher::*;
use std::time::Duration;
let mut cache = SimpleCacher::new(Duration::from_secs(60));
cache.insert("greeting".to_string(), "Hello, World!".to_string());
if let Ok(entry) = cache.get(&"greeting".to_string()) {
assert_eq!(entry.value(), "Hello, World!");
}Sourcepub fn value_mut(&mut self) -> &mut U
pub fn value_mut(&mut self) -> &mut U
Returns a mutable reference to the cached value.
§Examples
use simple_cacher::*;
use std::time::Duration;
let mut cache = SimpleCacher::new(Duration::from_secs(60));
cache.insert("counter".to_string(), 0u32);
if let Ok(entry) = cache.get_mut(&"counter".to_string()) {
*entry.value_mut() += 1;
assert_eq!(*entry.value(), 1);
}Sourcepub fn into_value(self) -> U
pub fn into_value(self) -> U
Consumes the cache object and returns the cached value.
§Examples
use simple_cacher::*;
use std::time::Duration;
let mut cache = SimpleCacher::new(Duration::from_secs(60));
cache.insert("data".to_string(), vec![1, 2, 3]);
if let Some(entry) = cache.remove(&"data".to_string()) {
let data = entry.into_value();
assert_eq!(data, vec![1, 2, 3]);
}Sourcepub fn age(&self) -> Duration
pub fn age(&self) -> Duration
Returns the age of this cache entry (time since creation).
§Examples
use simple_cacher::*;
use std::time::Duration;
let mut cache = SimpleCacher::new(Duration::from_secs(60));
cache.insert("key".to_string(), "value".to_string());
if let Ok(entry) = cache.get(&"key".to_string()) {
let age = entry.age();
assert!(age.as_millis() >= 0);
}Sourcepub fn created_at(&self) -> Instant
pub fn created_at(&self) -> Instant
Returns the instant when this entry was created.
§Examples
use simple_cacher::*;
use std::time::{Duration, Instant};
let mut cache = SimpleCacher::new(Duration::from_secs(60));
let before = Instant::now();
cache.insert("key".to_string(), "value".to_string());
let after = Instant::now();
if let Ok(entry) = cache.get(&"key".to_string()) {
let created = entry.created_at();
assert!(created >= before && created <= after);
}Trait Implementations§
Source§impl<U: Clone> Clone for SimpleCacheObject<U>
impl<U: Clone> Clone for SimpleCacheObject<U>
Source§fn clone(&self) -> SimpleCacheObject<U>
fn clone(&self) -> SimpleCacheObject<U>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl<U> Freeze for SimpleCacheObject<U>where
U: Freeze,
impl<U> RefUnwindSafe for SimpleCacheObject<U>where
U: RefUnwindSafe,
impl<U> Send for SimpleCacheObject<U>where
U: Send,
impl<U> Sync for SimpleCacheObject<U>where
U: Sync,
impl<U> Unpin for SimpleCacheObject<U>where
U: Unpin,
impl<U> UnwindSafe for SimpleCacheObject<U>where
U: UnwindSafe,
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
Mutably borrows from an owned value. Read more