Skip to main content

Module cache

Module cache 

Source
Expand description

Lance cache system.

§For cache users

Use LanceCache (or WeakLanceCache) to store and retrieve typed values. Define a CacheKey (or UnsizedCacheKey for trait objects) to describe what you’re caching and its type.

To make a value type serializable (so persistent backends can store it), implement CacheCodecImpl on the type, then override CacheKey::codec:

impl CacheCodecImpl for MyData {
    fn serialize(&self, w: &mut dyn Write) -> Result<()> { /* ... */ }
    fn deserialize(data: &Bytes) -> Result<Self> { /* ... */ }
}

impl CacheKey for MyDataKey {
    type ValueType = MyData;
    fn key(&self) -> Cow<'_, str> { /* ... */ }
    fn type_name() -> &'static str { "MyData" }
    fn codec() -> Option<CacheCodec> {
        Some(CacheCodec::from_impl::<MyData>())
    }
}

§For backend implementors

Implement CacheBackend to provide a custom storage layer (disk, Redis, etc.). Backends receive InternalCacheKey keys and type-erased CacheEntry values — the typed wrapping is handled by LanceCache. See the backend module for details.

§Serialization flow

When a CacheKey provides a codec via CacheKey::codec:

  1. LanceCache wraps the CacheCodec and passes it to the backend alongside the entry on insert and get calls.
  2. In-memory backends (like MokaCacheBackend) ignore the codec.
  3. Persistent backends use codec.serialize(entry, writer) on insert and codec.deserialize(reader) on get to persist entries across restarts.

Re-exports§

pub use backend::CacheBackend;
pub use backend::CacheEntry;
pub use backend::InternalCacheKey;
pub use codec::CacheCodec;
pub use codec::CacheCodecImpl;

Modules§

backend
Backend interface for cache implementors.
codec
Serialization codecs for cache entries.

Structs§

CacheStats
Context
The context of which references have already been seen. This should only be used in the implementation of the deep_size_of_children function, and (eventually, when this reaches 0.2) will not be able to be constructed, and only obtained from inside the function.
LanceCache
Typed cache wrapper that handles key construction and type safety.
MokaCacheBackend
Default CacheBackend backed by a moka cache.
WeakLanceCache
A weak reference to a LanceCache, used by indices to avoid circular references. When the original cache is dropped, operations on this will gracefully no-op.

Traits§

CacheKey
Typed cache key for sized value types.
DeepSizeOf
A trait for measuring the size of an object and its children
UnsizedCacheKey
Like CacheKey but for unsized value types (e.g. dyn Trait).

Derive Macros§

DeepSizeOf