Skip to main content

Module envelope

Module envelope 

Source
Expand description

The versioned envelope wrapped around every value written to a shared backend by 0.6.0 and later.

§Byte layout

 offset  size  field             encoding                      notes
 ------  ----  ----------------  ----------------------------  --------------------------------
      0     3  MAGIC             0x54 0x48 0x43  ("THC")       constant
      3     1  FORMAT_VERSION    0x01                          bumped only on envelope changes
      4     1  CODEC_ID          0x01 = postcard               0x02..=0x7F reserved by this crate
                                                               0x80..=0xFF free for user codecs
      5     8  expires_at_ms     u64 little-endian             ms since UNIX_EPOCH
     13     8  stale_until_ms    u64 little-endian             ms since UNIX_EPOCH
     21     N  payload           CacheCodec::encode(&entry)    N = buf.len() - 21
 ------  ----
     21        ENVELOPE_HEADER_LEN

There is no payload length field: the transport frames the value exactly (both Redis GET and memcached get return the stored length), and the codec detects truncation on its own.

Putting the timestamps in the header rather than in the codec payload keeps CacheCodec’s signature free of expiry concerns, makes the timings readable with redis-cli without running a codec, and removes the extra copy of the body that the 0.5.x Redis backend spent on double-encoding.

Constants§

CODEC_POSTCARD
Codec id of PostcardCodec.
CODEC_USER
Default codec id for codecs implemented outside this crate.
ENVELOPE_HEADER_LEN
Size of the envelope header in bytes.
FORMAT_V1
Envelope format version written by this release.
LEGACY_REDIS_OVERHEAD
Fixed overhead of the 0.5.x Redis outer record: an 8-byte u64 length prefix for the inner payload plus two 8-byte u64 timestamps.
MAGIC
Magic prefix identifying a 0.6.0 envelope: b"THC".

Functions§

decode_v2
Decodes an envelope written by this release.
is_legacy_redis
Exact structural test for a 0.5.x Redis value.
looks_like_v2
Reports whether bytes carries an envelope header this release understands.
read_stored
Reads a stored value, transparently accepting 0.5.x Redis entries.
unix_ms_to_system_time
Converts milliseconds since UNIX_EPOCH into a SystemTime, the encoding used by the envelope’s two timestamp fields.
wrap
Wraps an encoded payload in an envelope header.