1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! Cached value types with expiration metadata.
//!
//! This module provides types for wrapping cached data with expiration
//! and staleness timestamps:
//!
//! - [`CacheValue`] - Cached data with optional expire and stale timestamps
//! - [`CacheMeta`] - Just the metadata without the data
//!
//! ## Expiration vs Staleness
//!
//! Cache entries have two time-based states:
//!
//! - **Stale** - The data is still usable but should be refreshed in the background
//! - **Expired** - The data is no longer valid and must be refreshed before use
//!
//! This allows implementing "stale-while-revalidate" caching patterns where
//! stale data is served immediately while fresh data is fetched asynchronously.
//!
//! ## Cache States
//!
//! The [`CacheValue::cache_state`] method evaluates timestamps and returns:
//!
//! - [`CacheState::Actual`] - Data is fresh (neither stale nor expired)
//! - [`CacheState::Stale`] - Data is stale but not expired
//! - [`CacheState::Expired`] - Data has expired
//!
//! ```ignore
//! use hitbox_core::value::CacheValue;
//! use chrono::Utc;
//!
//! let value = CacheValue::new(
//! "cached data",
//! Some(Utc::now() + chrono::Duration::hours(1)), // expires in 1 hour
//! Some(Utc::now() + chrono::Duration::minutes(5)), // stale in 5 minutes
//! );
//!
//! match value.cache_state() {
//! CacheState::Actual(v) => println!("Fresh: {:?}", v.data()),
//! CacheState::Stale(v) => println!("Stale, refresh in background"),
//! CacheState::Expired(v) => println!("Expired, must refresh"),
//! }
//! ```
use ;
use size_of;
use Duration;
use crateRaw;
use crateCacheState;
/// 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();
/// ```
/// Cache expiration metadata without the data.
///
/// Contains just the staleness and expiration timestamps. Useful for
/// passing metadata around without copying the cached data.
///
/// # Fields
///
/// * `expire` - When the data expires (becomes invalid)
/// * `stale` - When the data becomes stale (should refresh in background)