|
1 | 1 | use crate::IOCached; |
| 2 | +use redis::Commands; |
2 | 3 | use serde::de::DeserializeOwned; |
3 | 4 | use serde::Serialize; |
4 | 5 | use std::marker::PhantomData; |
@@ -217,7 +218,7 @@ where |
217 | 218 | RedisCacheBuilder::new(prefix, ttl) |
218 | 219 | } |
219 | 220 |
|
220 | | - fn generate_key(&self, key: &K) -> String { |
| 221 | + fn generate_key(&self, key: impl Display) -> String { |
221 | 222 | format!("{}{}{}", self.namespace, self.prefix, key) |
222 | 223 | } |
223 | 224 |
|
@@ -345,6 +346,19 @@ where |
345 | 346 | Some(self.ttl) |
346 | 347 | } |
347 | 348 |
|
| 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 | + |
348 | 362 | fn cache_set_lifespan(&mut self, ttl: Duration) -> Option<Duration> { |
349 | 363 | let old = self.ttl; |
350 | 364 | self.ttl = ttl; |
@@ -628,6 +642,15 @@ mod async_redis { |
628 | 642 | } |
629 | 643 | } |
630 | 644 |
|
| 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 | + |
631 | 654 | /// Set the flag to control whether cache hits refresh the ttl of cached values, returns the old flag value |
632 | 655 | fn cache_set_refresh(&mut self, refresh: bool) -> bool { |
633 | 656 | let old = self.refresh; |
|
0 commit comments