native
Module[Native]: the codec of entries only Rust reads and writes.
Contents
use utopia_cache::Native;Rust
pub struct NativeTyped Rust values as compact binary entries, for caches only Rust reads and writes.
An entry is a header (a marker byte, N, and Native::VERSION), the
save time in unix seconds (8 bytes, little endian) and the payload. Bytes
without that header (an entry another codec or an older version wrote)
are a miss: the next save replaces them, nothing misreads them. Because
the payload carries no field names, change the field (or key) an entry is
stored under whenever its type changes.
Cache::load_native and
Cache::save_native store entries with it.
Rust
use serde::{Deserialize, Serialize};
use utopia_cache::Native;
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Team {
id: String,
members: Vec<String>,
}
let team = Team { id: "core".into(), members: vec!["ada".into(), "grace".into()] };
let stored = Native.encode(&team, 1_700_000_000)?;
// Fresh while `time + ttl > now`.
assert_eq!(Native.decode::<Team>(&stored, 60, 1_700_000_059), Some(team));
assert_eq!(Native.decode::<Team>(&stored, 60, 1_700_000_060), None);
// A JSON envelope is not a native entry: a miss, never a misread.
assert_eq!(Native.decode::<Team>(br#"{"time":1700000000,"data":"x"}"#, 60, 1_700_000_000), None);