pub struct PrefixedCache { /* private fields */ }Expand description
Prefixes every key, so several applications can share one cache server.
Worth reaching for whenever the cache is not exclusively yours. Two
applications caching user:1 on one Redis database will read each other’s
values — and the symptom is a user seeing another application’s data, not a
crash.
It also makes flush safe to mean “mine”: the inner cache’s
flush would empty the whole database, including the other application’s
keys and anybody’s sessions.
let shared: Arc<dyn Cache> = Arc::new(MemoryCache::new());
let billing = PrefixedCache::new(Arc::clone(&shared), "billing");
let catalogue = PrefixedCache::new(Arc::clone(&shared), "catalogue");
billing.put_string("total", "10", None).await?;
catalogue.put_string("total", "999", None).await?;
assert_eq!(billing.get_string("total").await?.as_deref(), Some("10"));Implementations§
Trait Implementations§
Source§impl Cache for PrefixedCache
impl Cache for PrefixedCache
Source§fn flush(&self) -> BoxFuture<'_, Result<()>>
fn flush(&self) -> BoxFuture<'_, Result<()>>
Not implemented as a flush.
There is no portable way to delete by prefix: Redis needs a SCAN loop
and Memcached cannot do it at all. Delegating to the inner flush would
empty the whole server, which is precisely what wrapping it was meant to
prevent — so this refuses instead of doing something surprising.
Source§fn get<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Result<Option<Vec<u8>>>>
fn get<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Result<Option<Vec<u8>>>>
key, or None if it is absent or expired.Source§fn put<'a>(
&'a self,
key: &'a str,
value: &'a [u8],
ttl: Option<Duration>,
) -> BoxFuture<'a, Result<()>>
fn put<'a>( &'a self, key: &'a str, value: &'a [u8], ttl: Option<Duration>, ) -> BoxFuture<'a, Result<()>>
value, expiring after ttl. None means no expiry.Source§fn forget<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Result<bool>>
fn forget<'a>(&'a self, key: &'a str) -> BoxFuture<'a, Result<bool>>
true if it was there.Source§fn increment<'a>(&'a self, key: &'a str, by: i64) -> BoxFuture<'a, Result<i64>>
fn increment<'a>(&'a self, key: &'a str, by: i64) -> BoxFuture<'a, Result<i64>>
by to a counter, creating it at zero first. Returns the new value. Read more