cache-kit 0.9.0

A type-safe, fully generic, production-ready caching framework for Rust
Documentation
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Postcard-based cache serialization with versioned envelopes.
//!
//! This module provides the canonical serialization format for all cache storage
//! in cache-kit. It uses Postcard for performance and wraps all cache entries in
//! versioned envelopes for schema evolution safety.
//!
//! # Architecture
//!
//! Every cache entry follows this format:
//! ```text
//! ┌─────────────────┬─────────────────┬──────────────────────────┐
//! │  MAGIC (4 bytes)│VERSION (4 bytes)│POSTCARD PAYLOAD (N bytes)│
//! └─────────────────┴─────────────────┴──────────────────────────┘
//!   "CKIT"              u32 (LE)           postcard::to_allocvec(T)
//! ```
//!
//! # Safety Guarantees
//!
//! - **Deterministic:** Same value always produces identical bytes
//! - **Validated:** Magic and version checked on every deserialization
//! - **Versioned:** Schema changes force cache eviction, not silent migration
//! - **Type-safe:** Postcard preserves exact Rust types
//!
//! # Example
//!
//! ```rust
//! use cache_kit::serialization::{serialize_for_cache, deserialize_from_cache};
//! use serde::{Serialize, Deserialize};
//!
//! #[derive(Serialize, Deserialize, PartialEq, Debug)]
//! struct User {
//!     id: u64,
//!     name: String,
//! }
//!
//! # fn main() -> cache_kit::Result<()> {
//! let user = User { id: 1, name: "Alice".to_string() };
//!
//! // Serialize with envelope
//! let bytes = serialize_for_cache(&user)?;
//!
//! // Deserialize with validation
//! let deserialized: User = deserialize_from_cache(&bytes)?;
//! assert_eq!(user, deserialized);
//! # Ok(())
//! # }
//! ```

use crate::error::{Error, Result};
use serde::{Deserialize, Serialize};

/// Magic header for cache-kit entries: b"CKIT"
///
/// This 4-byte signature identifies valid cache-kit cache entries.
/// Any entry without this magic is rejected during deserialization.
pub const CACHE_MAGIC: [u8; 4] = *b"CKIT";

/// Current schema version.
///
/// **CRITICAL:** Increment this constant when making breaking changes to cached types:
/// - Adding/removing struct fields
/// - Changing field types
/// - Reordering fields
/// - Changing enum variants
///
/// When deployed with a new version, old cache entries will be automatically
/// evicted and recomputed from the source of truth.
pub const CURRENT_SCHEMA_VERSION: u32 = 1;

/// Versioned envelope for cache entries.
///
/// Every cache entry is wrapped in this envelope to enable:
/// - **Corruption detection:** Invalid magic → reject entry
/// - **Schema evolution:** Version mismatch → evict and recompute
/// - **Observability:** Track version mismatches in metrics
///
/// # Format
///
/// ```text
/// ┌─────────────────┬─────────────────┬──────────────────────────┐
/// │  magic: [u8; 4] │ version: u32    │  payload: T              │
/// └─────────────────┴─────────────────┴──────────────────────────┘
/// ```
///
/// # Example
///
/// ```rust
/// use cache_kit::serialization::CacheEnvelope;
///
/// let envelope = CacheEnvelope::new("data");
/// assert_eq!(envelope.magic, *b"CKIT");
/// ```
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct CacheEnvelope<T> {
    /// Magic header: must be b"CKIT"
    pub magic: [u8; 4],
    /// Schema version: must match CURRENT_SCHEMA_VERSION
    pub version: u32,
    /// The actual cached data
    pub payload: T,
}

impl<T> CacheEnvelope<T> {
    /// Create a new envelope with current magic and version.
    ///
    /// # Example
    ///
    /// ```rust
    /// use cache_kit::serialization::CacheEnvelope;
    ///
    /// let envelope = CacheEnvelope::new(42);
    /// assert_eq!(envelope.payload, 42);
    /// ```
    pub fn new(payload: T) -> Self {
        Self {
            magic: CACHE_MAGIC,
            version: CURRENT_SCHEMA_VERSION,
            payload,
        }
    }
}

/// Serialize a value with envelope for cache storage.
///
/// This is the canonical way to serialize data for cache storage in cache-kit.
/// All cache backends (InMemory, Redis, Memcached) use this function.
///
/// # Format
///
/// ```text
/// [MAGIC: 4 bytes] [VERSION: 4 bytes] [POSTCARD PAYLOAD: N bytes]
/// ```
///
/// # Performance
///
/// Postcard serialization is approximately:
/// - **8-12x faster** than JSON serialization
/// - **50-70% smaller** than JSON payloads
///
/// # Example
///
/// ```rust
/// use cache_kit::serialization::serialize_for_cache;
/// use serde::Serialize;
///
/// #[derive(Serialize)]
/// struct User { id: u64, name: String }
///
/// # fn main() -> cache_kit::Result<()> {
/// let user = User { id: 1, name: "Alice".to_string() };
/// let bytes = serialize_for_cache(&user)?;
///
/// // Verify envelope structure
/// assert_eq!(&bytes[0..4], b"CKIT");
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns `Error::SerializationError` if Postcard serialization fails.
pub fn serialize_for_cache<T: Serialize>(value: &T) -> Result<Vec<u8>> {
    let envelope = CacheEnvelope::new(value);
    postcard::to_allocvec(&envelope).map_err(|e| {
        log::error!("Cache serialization failed: {}", e);
        Error::SerializationError(e.to_string())
    })
}

/// Deserialize a value from cache storage with validation.
///
/// This function performs strict validation:
/// 1. Checks magic header matches b"CKIT"
/// 2. Checks version matches CURRENT_SCHEMA_VERSION
/// 3. Deserializes Postcard payload
///
/// # Validation Strategy
///
/// **On magic mismatch:** Returns `Error::InvalidCacheEntry`
/// - Indicates corrupted cache entry or non-cache-kit data
/// - Cache entry should be evicted
///
/// **On version mismatch:** Returns `Error::VersionMismatch`
/// - Indicates schema change between code versions
/// - Cache entry should be evicted and recomputed
///
/// **On Postcard error:** Returns `Error::DeserializationError`
/// - Indicates corrupted payload
/// - Cache entry should be evicted
///
/// # Example
///
/// ```rust
/// use cache_kit::serialization::{serialize_for_cache, deserialize_from_cache};
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// struct User { id: u64, name: String }
///
/// # fn main() -> cache_kit::Result<()> {
/// let user = User { id: 1, name: "Alice".to_string() };
/// let bytes = serialize_for_cache(&user)?;
///
/// let deserialized: User = deserialize_from_cache(&bytes)?;
/// assert_eq!(user, deserialized);
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// - `Error::InvalidCacheEntry`: Invalid magic header
/// - `Error::VersionMismatch`: Schema version mismatch
/// - `Error::DeserializationError`: Corrupted Postcard payload
pub fn deserialize_from_cache<'de, T: Deserialize<'de>>(bytes: &'de [u8]) -> Result<T> {
    // Attempt to deserialize envelope
    let envelope: CacheEnvelope<T> = postcard::from_bytes(bytes).map_err(|e| {
        log::error!("Cache deserialization failed: {}", e);
        Error::DeserializationError(e.to_string())
    })?;

    // Validate magic header
    if envelope.magic != CACHE_MAGIC {
        log::warn!(
            "Invalid cache entry: expected magic {:?}, got {:?}",
            CACHE_MAGIC,
            envelope.magic
        );
        return Err(Error::InvalidCacheEntry(format!(
            "Invalid magic: expected {:?}, got {:?}",
            CACHE_MAGIC, envelope.magic
        )));
    }

    // Validate schema version
    if envelope.version != CURRENT_SCHEMA_VERSION {
        log::warn!(
            "Cache version mismatch: expected {}, got {}",
            CURRENT_SCHEMA_VERSION,
            envelope.version
        );
        return Err(Error::VersionMismatch {
            expected: CURRENT_SCHEMA_VERSION,
            found: envelope.version,
        });
    }

    Ok(envelope.payload)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
    struct TestData {
        id: u64,
        name: String,
        active: bool,
    }

    #[test]
    fn test_roundtrip() {
        let data = TestData {
            id: 123,
            name: "test".to_string(),
            active: true,
        };

        let bytes = serialize_for_cache(&data).unwrap();
        let deserialized: TestData = deserialize_from_cache(&bytes).unwrap();

        assert_eq!(data, deserialized);
    }

    #[test]
    fn test_envelope_structure() {
        let data = TestData {
            id: 123,
            name: "test".to_string(),
            active: true,
        };

        let bytes = serialize_for_cache(&data).unwrap();

        // Deserialize the envelope to verify its structure
        // (postcard uses variable-length encoding, so we can't rely on fixed byte positions)
        let envelope: CacheEnvelope<TestData> = postcard::from_bytes(&bytes).unwrap();

        // Verify envelope magic
        assert_eq!(envelope.magic, CACHE_MAGIC);

        // Verify envelope version
        assert_eq!(envelope.version, CURRENT_SCHEMA_VERSION);

        // Verify payload matches original data
        assert_eq!(envelope.payload, data);
    }

    #[test]
    fn test_envelope_new() {
        let envelope = CacheEnvelope::new(42);
        assert_eq!(envelope.magic, CACHE_MAGIC);
        assert_eq!(envelope.version, CURRENT_SCHEMA_VERSION);
        assert_eq!(envelope.payload, 42);
    }

    #[test]
    fn test_invalid_magic_rejected() {
        let mut bytes = vec![0u8; 100];
        bytes[0..4].copy_from_slice(b"XXXX"); // Wrong magic
        bytes[4..8].copy_from_slice(&1u32.to_le_bytes()); // Valid version

        let result: Result<TestData> = deserialize_from_cache(&bytes);
        assert!(result.is_err());
        match result.unwrap_err() {
            Error::InvalidCacheEntry(_) => {} // Expected
            e => panic!("Expected InvalidCacheEntry, got {:?}", e),
        }
    }

    #[test]
    fn test_version_mismatch_rejected() {
        let data = TestData {
            id: 123,
            name: "test".to_string(),
            active: true,
        };

        let mut envelope = CacheEnvelope::new(&data);
        envelope.version = 999; // Future version

        let bytes = postcard::to_allocvec(&envelope).unwrap();
        let result: Result<TestData> = deserialize_from_cache(&bytes);

        assert!(result.is_err());
        match result.unwrap_err() {
            Error::VersionMismatch { expected, found } => {
                assert_eq!(expected, CURRENT_SCHEMA_VERSION);
                assert_eq!(found, 999);
            }
            e => panic!("Expected VersionMismatch, got {:?}", e),
        }
    }

    #[test]
    fn test_deterministic_serialization() {
        let data1 = TestData {
            id: 123,
            name: "test".to_string(),
            active: true,
        };
        let data2 = data1.clone();

        let bytes1 = serialize_for_cache(&data1).unwrap();
        let bytes2 = serialize_for_cache(&data2).unwrap();

        // Must produce identical bytes
        assert_eq!(bytes1, bytes2);
    }

    #[test]
    fn test_corrupted_payload_rejected() {
        // Create a valid envelope first
        let data = TestData {
            id: 123,
            name: "test".to_string(),
            active: true,
        };
        let mut bytes = serialize_for_cache(&data).unwrap();

        // Corrupt the payload by truncating aggressively
        // With postcard's compact encoding, we need to truncate enough to ensure
        // the data structure is incomplete
        let original_len = bytes.len();
        bytes.truncate(original_len / 2); // Truncate to half the size

        let result: Result<TestData> = deserialize_from_cache(&bytes);
        assert!(result.is_err());
        match result.unwrap_err() {
            Error::DeserializationError(_) => {} // Expected
            e => panic!("Expected DeserializationError, got {:?}", e),
        }
    }

    #[test]
    fn test_empty_data_roundtrip() {
        let data = TestData {
            id: 0,
            name: String::new(),
            active: false,
        };

        let bytes = serialize_for_cache(&data).unwrap();
        let deserialized: TestData = deserialize_from_cache(&bytes).unwrap();

        assert_eq!(data, deserialized);
    }

    #[test]
    fn test_large_data_roundtrip() {
        let data = TestData {
            id: u64::MAX,
            name: "x".repeat(10000),
            active: true,
        };

        let bytes = serialize_for_cache(&data).unwrap();
        let deserialized: TestData = deserialize_from_cache(&bytes).unwrap();

        assert_eq!(data, deserialized);
    }

    #[test]
    fn test_postcard_smaller_than_json() {
        let data = TestData {
            id: 123,
            name: "test".to_string(),
            active: true,
        };

        let postcard_bytes = serialize_for_cache(&data).unwrap();
        let json_bytes = serde_json::to_vec(&data).unwrap();

        // Postcard should be smaller (including envelope overhead)
        assert!(
            postcard_bytes.len() < json_bytes.len(),
            "Postcard ({} bytes) should be smaller than JSON ({} bytes)",
            postcard_bytes.len(),
            json_bytes.len()
        );
    }
}