Skip to content

Commit 6cdcd56

Browse files
committed
Add cache_clear operation
1 parent 51280cf commit 6cdcd56

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,13 @@ pub trait IOCached<K, V> {
445445
/// Should return `Self::Error` if the operation fails
446446
fn cache_remove(&self, k: &K) -> Result<Option<V>, Self::Error>;
447447

448+
/// Remove all cached values
449+
///
450+
/// # Errors
451+
///
452+
/// Should return `Self::Error` if the operation fails
453+
fn cache_clear(&self) -> Result<(), Self::Error>;
454+
448455
/// Set the flag to control whether cache hits refresh the ttl of cached values, returns the old flag value
449456
fn cache_set_refresh(&mut self, refresh: bool) -> bool;
450457

@@ -479,6 +486,9 @@ pub trait IOCachedAsync<K, V> {
479486
/// Remove a cached value
480487
async fn cache_remove(&self, k: &K) -> Result<Option<V>, Self::Error>;
481488

489+
/// Remove all cached values
490+
async fn cache_clear(&self) -> Result<(), Self::Error>;
491+
482492
/// Set the flag to control whether cache hits refresh the ttl of cached values, returns the old flag value
483493
fn cache_set_refresh(&mut self, refresh: bool) -> bool;
484494

src/stores/disk.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,13 @@ where
353353
self.ttl
354354
}
355355

356+
fn cache_clear(&self) -> Result<(), Self::Error> {
357+
for (key, value) in self.connection.iter().flatten() {
358+
self.connection.remove(key)?;
359+
}
360+
Ok(())
361+
}
362+
356363
fn cache_set_lifespan(&mut self, ttl: Duration) -> Option<Duration> {
357364
let old = self.ttl;
358365
self.ttl = Some(ttl);

src/stores/redis.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::IOCached;
2+
use redis::Commands;
23
use serde::de::DeserializeOwned;
34
use serde::Serialize;
45
use std::marker::PhantomData;
@@ -217,7 +218,7 @@ where
217218
RedisCacheBuilder::new(prefix, ttl)
218219
}
219220

220-
fn generate_key(&self, key: &K) -> String {
221+
fn generate_key(&self, key: impl Display) -> String {
221222
format!("{}{}{}", self.namespace, self.prefix, key)
222223
}
223224

@@ -345,6 +346,19 @@ where
345346
Some(self.ttl)
346347
}
347348

349+
fn cache_clear(&self) -> Result<(), Self::Error> {
350+
// `scan_match` takes `&mut self`, so we need two connection objects to scan and
351+
// delete...?
352+
let mut scan = self.pool.get()?;
353+
let mut delete = self.pool.get()?;
354+
355+
for key in scan.scan_match::<_, String>(self.generate_key("*"))? {
356+
delete.del(key)?;
357+
}
358+
359+
Ok(())
360+
}
361+
348362
fn cache_set_lifespan(&mut self, ttl: Duration) -> Option<Duration> {
349363
let old = self.ttl;
350364
self.ttl = ttl;
@@ -628,6 +642,15 @@ mod async_redis {
628642
}
629643
}
630644

645+
async fn cache_clear(&self) -> Result<(), Self::Error> {
646+
let mut conn = self.connection.clone();
647+
648+
// https://redis.io/commands/flushdb/
649+
let _: () = redis::cmd("FLUSHDB").query_async(&mut conn).await?;
650+
651+
Ok(())
652+
}
653+
631654
/// Set the flag to control whether cache hits refresh the ttl of cached values, returns the old flag value
632655
fn cache_set_refresh(&mut self, refresh: bool) -> bool {
633656
let old = self.refresh;

0 commit comments

Comments
 (0)