tork_core/cache/store.rs
1//! The pluggable cache backend.
2
3use std::time::Duration;
4
5use crate::error::Result;
6use crate::router::BoxFuture;
7
8/// A cache backend that stores opaque byte values under string keys.
9///
10/// The [`Cache`](crate::Cache) handle serializes typed values to bytes and talks
11/// to a store through this trait, so any backend (in-memory, Redis) works the same
12/// from a handler's point of view. The trait is object-safe: a [`Cache`] holds an
13/// `Arc<dyn CacheStore>`.
14///
15/// TTL convention: `None` keeps the entry until the store evicts it (no explicit
16/// expiry); `Some(duration)` expires the entry after `duration`. A zero duration
17/// is normalized to `None` ("never expire") by the [`Cache`](crate::Cache) handle
18/// before it reaches the store.
19pub trait CacheStore: Send + Sync + 'static {
20 /// Returns the bytes stored under `key`, or `None` if absent or expired.
21 fn get<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Result<Option<Vec<u8>>>>;
22
23 /// Stores `value` under `key`, expiring it after `ttl` when set.
24 fn set(&self, key: String, value: Vec<u8>, ttl: Option<Duration>) -> BoxFuture<'_, Result<()>>;
25
26 /// Removes the entry under `key`, if any.
27 fn delete<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Result<()>>;
28
29 /// Removes every entry from the store.
30 fn clear(&self) -> BoxFuture<'_, Result<()>>;
31}