pub trait CacheStore: Send + Sync {
// Required methods
fn get(&self, key: &CacheKey) -> Result<Option<Value>>;
fn put(&self, key: &CacheKey, value: &Value) -> Result<()>;
fn exists(&self, key: &CacheKey) -> Result<bool>;
fn remove(&self, key: &CacheKey) -> Result<()>;
fn metadata(&self, key: &CacheKey) -> Result<Option<EntryMeta>>;
// Provided methods
fn put_with_origin(
&self,
key: &CacheKey,
value: &Value,
origin: &Origin,
) -> Result<()> { ... }
fn put_computed(
&self,
key: &CacheKey,
value: &Value,
origin: &Origin,
compute: Duration,
deterministic: bool,
) -> Result<()> { ... }
fn tier(&self) -> CacheTier { ... }
fn get_located(&self, key: &CacheKey) -> Result<Option<(Value, CacheTier)>> { ... }
}Expand description
The K/V cache store interface.
Implementations may be in-memory, on-disk (RocksDB/sled), or remote (S3). The tiered cache composes multiple stores.
Required Methods§
Sourcefn get(&self, key: &CacheKey) -> Result<Option<Value>>
fn get(&self, key: &CacheKey) -> Result<Option<Value>>
Look up the value stored under key, None on a miss.
Sourcefn put(&self, key: &CacheKey, value: &Value) -> Result<()>
fn put(&self, key: &CacheKey, value: &Value) -> Result<()>
Store value under key, replacing any existing entry.
Sourcefn exists(&self, key: &CacheKey) -> Result<bool>
fn exists(&self, key: &CacheKey) -> Result<bool>
Whether key has an entry, without loading the value.
Provided Methods§
Sourcefn put_with_origin(
&self,
key: &CacheKey,
value: &Value,
origin: &Origin,
) -> Result<()>
fn put_with_origin( &self, key: &CacheKey, value: &Value, origin: &Origin, ) -> Result<()>
Store a value together with its provenance. Stores that persist metadata should override this; the default discards the origin.
Sourcefn put_computed(
&self,
key: &CacheKey,
value: &Value,
origin: &Origin,
compute: Duration,
deterministic: bool,
) -> Result<()>
fn put_computed( &self, key: &CacheKey, value: &Value, origin: &Origin, compute: Duration, deterministic: bool, ) -> Result<()>
Store a freshly-computed value with its full provenance record: origin, wall-clock compute cost, and the producer’s determinism declaration. Cost-aware eviction needs the compute time — a tiny value that took days must outlive a huge one that took seconds. The default discards the extra metadata.
Sourcefn tier(&self) -> CacheTier
fn tier(&self) -> CacheTier
Which tier this store is, for reporting.
A single-tier store answers with its own kind. CacheTier::Memory
is the default because the in-memory store is the one people write
by hand; a store that is anything else should say so.
Sourcefn get_located(&self, key: &CacheKey) -> Result<Option<(Value, CacheTier)>>
fn get_located(&self, key: &CacheKey) -> Result<Option<(Value, CacheTier)>>
Like CacheStore::get, but also reports which tier served the value.
A composed store overrides this — that is the whole point. Without it, a hit served from disk is indistinguishable from one served from RAM, and the numbers that are supposed to tell you whether the disk tier is earning its keep say only that the cache was used.