Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions rust/lance-core/src/cache/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use futures::Future;

use crate::Result;

use super::ErasedCodec;

/// A type-erased cache entry.
pub type CacheEntry = Arc<dyn Any + Send + Sync>;

Expand Down Expand Up @@ -74,10 +76,26 @@ impl InternalCacheKey {
#[async_trait]
pub trait CacheBackend: Send + Sync + std::fmt::Debug {
/// Look up an entry by its key.
async fn get(&self, key: &InternalCacheKey) -> Option<CacheEntry>;
///
/// `codec` is provided so that backends with persistent storage can
/// deserialize the entry. In-memory backends can ignore it.
async fn get(
&self,
key: &InternalCacheKey,
codec: Option<&Arc<ErasedCodec>>,
) -> Option<CacheEntry>;

/// Store an entry. `size_bytes` is used for eviction accounting.
async fn insert(&self, key: &InternalCacheKey, entry: CacheEntry, size_bytes: usize);
///
/// `codec` is provided so that backends with persistent storage can
/// serialize the entry. In-memory backends can ignore it.
async fn insert(
&self,
key: &InternalCacheKey,
entry: CacheEntry,
size_bytes: usize,
codec: Option<&Arc<ErasedCodec>>,
);

/// Get an existing entry or compute it from `loader`.
///
Expand All @@ -86,10 +104,13 @@ pub trait CacheBackend: Send + Sync + std::fmt::Debug {
///
/// Returns `(entry, was_cached)` where `was_cached` is `true` if the entry
/// was already present in the cache (the loader was not invoked).
///
/// `codec` is provided for backends that need to serialize/deserialize.
async fn get_or_insert<'a>(
&self,
key: &InternalCacheKey,
loader: Pin<Box<dyn Future<Output = Result<(CacheEntry, usize)>> + Send + 'a>>,
codec: Option<&Arc<ErasedCodec>>,
) -> Result<(CacheEntry, bool)>;

/// Remove all entries whose prefix starts with the given string.
Expand Down
Loading
Loading