Rust

codec

Module

[Codec]: how a cache value becomes the bytes an adapter stores.

Contents

use utopia_cache::Igbinary;
Rust
pub struct Igbinary

Binary 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.

Rust
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;
Rust
pub struct Json

JSON, 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 [].

Rust
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;
Rust
pub enum AnyCodec

A codec chosen at run time, for configuration-driven setups.

use utopia_cache::Codec;
Rust
pub trait Codec: Send + Sync + 'static

Turns 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.

Implemented byJsonIgbinaryAnyCodec