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>>,
) -> CacheValue<T>
pub fn new( data: T, expire: Option<DateTime<Utc>>, stale: Option<DateTime<Utc>>, ) -> CacheValue<T>
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<CacheValue<T>>
pub fn cache_state(self) -> CacheState<CacheValue<T>>
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<Bytes>
impl CacheValue<Bytes>
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 for CacheValue<T>where
T: Clone,
impl<T> Clone for CacheValue<T>where
T: Clone,
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 moreSource§impl<T> Debug for CacheValue<T>where
T: Debug,
impl<T> Debug for CacheValue<T>where
T: Debug,
Source§impl<T> PartialEq for CacheValue<T>where
T: PartialEq,
impl<T> PartialEq for CacheValue<T>where
T: PartialEq,
impl<T> Eq for CacheValue<T>where
T: Eq,
impl<T> StructuralPartialEq for CacheValue<T>
Auto Trait Implementations§
impl<T> Freeze for CacheValue<T>where
T: Freeze,
impl<T> RefUnwindSafe for CacheValue<T>where
T: RefUnwindSafe,
impl<T> Send for CacheValue<T>where
T: Send,
impl<T> Sync for CacheValue<T>where
T: Sync,
impl<T> Unpin for CacheValue<T>where
T: Unpin,
impl<T> UnwindSafe for CacheValue<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.