Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,13 @@ pub trait IOCached<K, V> {
/// Should return `Self::Error` if the operation fails
fn cache_remove(&self, k: &K) -> Result<Option<V>, Self::Error>;

/// Remove all cached values
///
/// # Errors
///
/// Should return `Self::Error` if the operation fails
fn cache_clear(&self) -> Result<(), Self::Error>;

/// Set the flag to control whether cache hits refresh the ttl of cached values, returns the old flag value
fn cache_set_refresh(&mut self, refresh: bool) -> bool;

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

/// Remove all cached values
async fn cache_clear(&self) -> Result<(), Self::Error>;

/// Set the flag to control whether cache hits refresh the ttl of cached values, returns the old flag value
fn cache_set_refresh(&mut self, refresh: bool) -> bool;

Expand Down
7 changes: 7 additions & 0 deletions src/stores/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,13 @@ where
self.ttl
}

fn cache_clear(&self) -> Result<(), Self::Error> {
for (key, _value) in self.connection.iter().flatten() {
self.connection.remove(key)?;
}
Ok(())
}

fn cache_set_lifespan(&mut self, ttl: Duration) -> Option<Duration> {
let old = self.ttl;
self.ttl = Some(ttl);
Expand Down
35 changes: 33 additions & 2 deletions src/stores/redis.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::IOCached;
use redis::Commands;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::marker::PhantomData;
Expand Down Expand Up @@ -217,7 +218,7 @@ where
RedisCacheBuilder::new(prefix, ttl)
}

fn generate_key(&self, key: &K) -> String {
fn generate_key(&self, key: impl Display) -> String {
format!("{}{}{}", self.namespace, self.prefix, key)
}

Expand Down Expand Up @@ -345,6 +346,19 @@ where
Some(self.ttl)
}

fn cache_clear(&self) -> Result<(), RedisCacheError> {
// `scan_match` takes `&mut self`, so we need two connection objects to scan and
// delete...?
let mut scan = self.pool.get()?;
let mut delete = self.pool.get()?;

for key in scan.scan_match::<_, String>(self.generate_key("*"))? {
let () = delete.del(key)?;
}

Ok(())
}

fn cache_set_lifespan(&mut self, ttl: Duration) -> Option<Duration> {
let old = self.ttl;
self.ttl = ttl;
Expand All @@ -365,6 +379,8 @@ where
mod async_redis {
use std::time::Duration;

use redis::AsyncCommands;

use super::{
CachedRedisValue, DeserializeOwned, Display, PhantomData, RedisCacheBuildError,
RedisCacheError, Serialize, DEFAULT_NAMESPACE, ENV_KEY,
Expand Down Expand Up @@ -530,7 +546,7 @@ mod async_redis {
AsyncRedisCacheBuilder::new(prefix, ttl)
}

fn generate_key(&self, key: &K) -> String {
fn generate_key(&self, key: impl Display) -> String {
format!("{}{}{}", self.namespace, self.prefix, key)
}

Expand Down Expand Up @@ -628,6 +644,21 @@ mod async_redis {
}
}

async fn cache_clear(&self) -> Result<(), Self::Error> {
// `scan_match` takes `&mut self`, so we need two connection objects to scan and
// delete...?
let mut scan = self.connection.clone();
let mut delete = self.connection.clone();

let mut scanner = scan.scan_match::<_, String>(self.generate_key("*")).await?;

while let Some(key) = scanner.next_item().await {
let () = delete.del(key).await?;
}

Ok(())
}

/// Set the flag to control whether cache hits refresh the ttl of cached values, returns the old flag value
fn cache_set_refresh(&mut self, refresh: bool) -> bool {
let old = self.refresh;
Expand Down
Loading