Struct SimpleCacheObject

Source
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>

Source

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());
}
Source

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!");
}
Source

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);
}
Source

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]);
}
Source

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);
}
Source

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);
}

Auto 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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.