pub struct CacheValue<T> { /* private fields */ }Expand description
A cached value with expiration metadata.
Wraps any data type T with optional timestamps for staleness and expiration.
This enables time-based cache invalidation and stale-while-revalidate patterns.
§Type Parameter
T- The cached data type
§Example
use hitbox_core::value::CacheValue;
use chrono::Utc;
use std::time::Duration;
// Create a cache value that expires in 1 hour
let expire_time = Utc::now() + chrono::Duration::hours(1);
let value = CacheValue::new("user_data", Some(expire_time), None);
// Access data via getter
assert_eq!(value.data(), &"user_data");
// Check remaining TTL
if let Some(ttl) = value.ttl() {
println!("Expires in {} seconds", ttl.as_secs());
}
// Extract the data
let data = value.into_inner();Implementations§
Source§impl<T> CacheValue<T>
impl<T> CacheValue<T>
Sourcepub fn new(
data: T,
expire: Option<DateTime<Utc>>,
stale: Option<DateTime<Utc>>,
) -> Self
pub fn new( data: T, expire: Option<DateTime<Utc>>, stale: Option<DateTime<Utc>>, ) -> Self
Creates a new cache value with the given data and timestamps.
§Arguments
data- The data to cacheexpire- When the data expires (becomes invalid)stale- When the data becomes stale (should refresh in background)
Sourcepub fn stale(&self) -> Option<DateTime<Utc>>
pub fn stale(&self) -> Option<DateTime<Utc>>
Returns when the data becomes stale (should refresh in background).
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the cache value and returns the inner data.
Discards the expiration metadata.
Sourcepub fn into_parts(self) -> (CacheMeta, T)
pub fn into_parts(self) -> (CacheMeta, T)
Consumes the cache value and returns metadata and data separately.
Useful when you need to inspect or modify the metadata independently.
Source§impl<T> CacheValue<T>
impl<T> CacheValue<T>
Sourcepub fn cache_state(self) -> CacheState<Self>
pub fn cache_state(self) -> CacheState<Self>
Check the cache state based on expire/stale timestamps.
Returns CacheState<CacheValue<T>> preserving the original value with metadata.
This is a sync operation - just checks timestamps, no conversion.
The caller is responsible for converting to Response via from_cached() when needed.
Source§impl CacheValue<Raw>
impl CacheValue<Raw>
Sourcepub fn memory_size(&self) -> usize
pub fn memory_size(&self) -> usize
Returns the estimated memory usage of this cache value in bytes.
This includes:
- Fixed struct overhead (CacheValue fields)
- The serialized data bytes
Trait Implementations§
Source§impl<T: Clone> Clone for CacheValue<T>
impl<T: Clone> Clone for CacheValue<T>
Source§fn clone(&self) -> CacheValue<T>
fn clone(&self) -> CacheValue<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more