Skip to main content

CacheKey

Trait CacheKey 

Source
pub trait CacheKey {
    type ValueType: 'static;

    // Required methods
    fn key(&self) -> Cow<'_, str>;
    fn type_name() -> &'static str;

    // Provided methods
    fn stable_type_id() -> &'static str { ... }
    fn schema() -> CacheKeySchema { ... }
    fn write_key(&self, builder: &mut KeyBuilder) { ... }
    fn codec() -> Option<CacheCodec> { ... }
}
Expand description

Typed cache key for sized value types.

Existing implementations can continue returning a logical string from key. Performance-sensitive implementations should also provide a stable schema and stream typed fields through write_key, avoiding construction of that string.

§Example

struct MyKey { id: u64 }

impl CacheKey for MyKey {
    type ValueType = MyData;
    fn key(&self) -> Cow<'_, str> { self.id.to_string().into() }
    fn type_name() -> &'static str { "MyData" }
}

Required Associated Types§

Source

type ValueType: 'static

Required Methods§

Source

fn key(&self) -> Cow<'_, str>

Source

fn type_name() -> &'static str

Short, stable string identifying this value type.

Two CacheKey impls that store different ValueTypes must return different type names.

Use a short literal (e.g. "Vec<IndexMetadata>"), not std::any::type_name — the latter is not guaranteed stable across compiler versions or build configurations.

Provided Methods§

Source

fn stable_type_id() -> &'static str

Stable identity included in the physical key.

The compatibility default preserves existing implementations by using their author-assigned type_name.

Source

fn schema() -> CacheKeySchema

Versioned schema for the logical key fields.

Source

fn write_key(&self, builder: &mut KeyBuilder)

Stream the logical key fields into the canonical key builder.

The compatibility default hashes the existing string key. In-tree hot paths override this with typed, allocation-free field encoding.

Source

fn codec() -> Option<CacheCodec>

Optional codec for serializing/deserializing this key’s value type.

Returns None by default. Cache backends that support persistence (e.g. disk-backed caches) use this to serialize entries on insert and deserialize on get. Types without a codec will only be stored in-memory.

CacheCodec is Copy (two plain function pointers), so returning it by value is cheap — no allocation needed.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§