pub struct TieredCache<L1, L2>where
L1: CacheStore,
L2: CacheStore,{ /* private fields */ }Expand description
Multi-tier cache with L1 (in-memory) and L2 (distributed) layers
Implementations§
Source§impl<L1, L2> TieredCache<L1, L2>where
L1: CacheStore,
L2: CacheStore,
impl<L1, L2> TieredCache<L1, L2>where
L1: CacheStore,
L2: CacheStore,
Sourcepub fn with_config(l1: Arc<L1>, l2: Arc<L2>, config: TieredCacheConfig) -> Self
pub fn with_config(l1: Arc<L1>, l2: Arc<L2>, config: TieredCacheConfig) -> Self
Create with custom configuration
Sourcepub async fn get(&self, key: &str) -> CacheResult<Option<String>>
pub async fn get(&self, key: &str) -> CacheResult<Option<String>>
Get value from cache (checks L1 then L2)
Sourcepub async fn set(
&self,
key: &str,
value: String,
ttl: Option<Duration>,
) -> CacheResult<()>
pub async fn set( &self, key: &str, value: String, ttl: Option<Duration>, ) -> CacheResult<()>
Set value in cache (writes to both L1 and L2)
Sourcepub async fn delete(&self, key: &str) -> CacheResult<()>
pub async fn delete(&self, key: &str) -> CacheResult<()>
Delete from both L1 and L2
Sourcepub async fn exists(&self, key: &str) -> CacheResult<bool>
pub async fn exists(&self, key: &str) -> CacheResult<bool>
Check if key exists (checks L1 then L2)
Sourcepub async fn clear(&self) -> CacheResult<()>
pub async fn clear(&self) -> CacheResult<()>
Clear both L1 and L2
L2::clear() is whatever the L2 backend’s CacheStore::clear() does.
For RedisCache with a key_prefix configured, that is now scoped:
it SCANs for and UNLINKs only the keys under that prefix, not the
whole database. Remaining risk: an L2 RedisCache with no
key_prefix configured has no distinct slice of the keyspace to
scope to and still falls back to unscoped FLUSHDB, wiping the
entire Redis database/instance — so a TieredCache wrapping an
unprefixed RedisCache that shares a Redis instance with other
services/tenants should not call clear(). A MemcachedCache L2 has
no prefix-scoped clear at all (the memcached protocol has no key
enumeration primitive), so its clear() always wipes the whole
memcached instance regardless of key_prefix.
Sourcepub async fn stats(&self) -> CacheStats
pub async fn stats(&self) -> CacheStats
Get cache statistics.
The *_enabled/write_through/promote_to_l1 fields echo the static
configuration; the l1_hits/l2_hits/misses/promotions counters are
live totals accumulated across every get since construction (shared
across clones).