Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache_clear operation #200

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 @@ -410,6 +410,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 All @@ -436,6 +443,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 @@ -269,6 +269,13 @@ where
}
}

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

fn cache_lifespan(&self) -> Option<u64> {
self.seconds
}
Expand Down
25 changes: 24 additions & 1 deletion 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::fmt::Display;
Expand Down Expand Up @@ -217,7 +218,7 @@ where
RedisCacheBuilder::new(prefix, seconds)
}

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 @@ -341,6 +342,19 @@ where
}
}

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.pool.get()?;
let mut delete = self.pool.get()?;

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

Ok(())
}

fn cache_lifespan(&self) -> Option<u64> {
Some(self.seconds)
}
Expand Down Expand Up @@ -626,6 +640,15 @@ mod async_redis {
}
}

async fn cache_clear(&self) -> Result<(), Self::Error> {
let mut conn = self.connection.clone();

// https://redis.io/commands/flushdb/
let _: () = redis::cmd("FLUSHDB").query_async(&mut conn).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