Skip to content

Commit 7d2342b

Browse files
committed
Add cache_clear operation
1 parent 51280cf commit 7d2342b

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
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: 33 additions & 2 deletions
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<(), RedisCacheError> {
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+
let () = 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;
@@ -365,6 +379,8 @@ where
365379
mod async_redis {
366380
use std::time::Duration;
367381

382+
use redis::AsyncCommands;
383+
368384
use super::{
369385
CachedRedisValue, DeserializeOwned, Display, PhantomData, RedisCacheBuildError,
370386
RedisCacheError, Serialize, DEFAULT_NAMESPACE, ENV_KEY,
@@ -530,7 +546,7 @@ mod async_redis {
530546
AsyncRedisCacheBuilder::new(prefix, ttl)
531547
}
532548

533-
fn generate_key(&self, key: &K) -> String {
549+
fn generate_key(&self, key: impl Display) -> String {
534550
format!("{}{}{}", self.namespace, self.prefix, key)
535551
}
536552

@@ -628,6 +644,21 @@ mod async_redis {
628644
}
629645
}
630646

647+
async fn cache_clear(&self) -> Result<(), Self::Error> {
648+
// `scan_match` takes `&mut self`, so we need two connection objects to scan and
649+
// delete...?
650+
let mut scan = self.connection.clone();
651+
let mut delete = self.connection.clone();
652+
653+
let mut scanner = scan.scan_match::<_, String>(self.generate_key("*")).await?;
654+
655+
while let Some(key) = scanner.next_item().await {
656+
let () = delete.del(key).await?;
657+
}
658+
659+
Ok(())
660+
}
661+
631662
/// Set the flag to control whether cache hits refresh the ttl of cached values, returns the old flag value
632663
fn cache_set_refresh(&mut self, refresh: bool) -> bool {
633664
let old = self.refresh;

0 commit comments

Comments
 (0)