codec
Module[Codec]: how a cache value becomes the bytes an adapter stores.
Contents
use utopia_cache::Igbinary;pub struct IgbinaryBinary igbinary payloads: smaller and faster than JSON, and a lossless round trip for every value, including empty objects.
Payloads are not JSON, so switching an existing cache to this codec turns its stored entries into misses until they are rewritten.
use php_std::types::Value;
use utopia_cache::{Codec, Igbinary};
let bytes = Igbinary.encode(&Value::from("hello"))?;
assert_eq!(Igbinary.decode(&bytes)?, Value::from("hello"));
assert!(Igbinary.decode(b"").is_err());use utopia_cache::Json;pub struct JsonJSON, the format every adapter has always written.
Objects decode to associative arrays, except empty ones, which stay
objects so {} survives a round trip instead of becoming [].
use php_std::types::{Array, Value};
use utopia_cache::{Codec, Json};
let value = Value::Array(Array::from_entries([("time".into(), Value::Int(1)), ("data".into(), "x".into())], None));
assert_eq!(Json.encode(&value)?, br#"{"time":1,"data":"x"}"#);
assert_eq!(Json.decode(br#"{"time":1,"data":"x"}"#)?, value);use utopia_cache::AnyCodec;pub enum AnyCodecA codec chosen at run time, for configuration-driven setups.
use utopia_cache::Codec;pub trait Codec: Send + Sync + 'staticTurns a cache value into bytes and back.
Both directions fail on bad input: a value that cannot be encoded, or bytes that are not a payload this codec wrote. Adapters treat a decode failure as a miss. Implement it to store values in another format.