serialize_for_cache

Function serialize_for_cache 

Source
pub fn serialize_for_cache<T: Serialize>(value: &T) -> Result<Vec<u8>>
Expand description

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

[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

use cache_kit::serialization::serialize_for_cache;
use serde::Serialize;

#[derive(Serialize)]
struct User { id: u64, name: String }

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

§Errors

Returns Error::SerializationError if Postcard serialization fails.