-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for redis as cache storage
- Loading branch information
Fabian Triebsch
committed
Apr 1, 2024
1 parent
20d6c90
commit 3d4f475
Showing
23 changed files
with
414 additions
and
96 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod compression; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod common; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub(crate) mod cache; | ||
mod provider; | ||
mod file_provider; | ||
mod redis_provider; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use std::io; | ||
|
||
use redis::{Commands, Expiry, RedisError}; | ||
|
||
use super::provider::CacheProvider; | ||
|
||
pub struct RedisProvider { | ||
update: bool, | ||
panic_on_cache_content_mismatch: bool, | ||
expire: Option<u32>, | ||
client: redis::Client | ||
} | ||
|
||
impl RedisProvider { | ||
pub fn new(url: &str, update: bool, panic_on_cache_content_mismatch: bool, expire: Option<u32>) -> RedisProvider { | ||
let client = redis::Client::open(url).unwrap(); | ||
|
||
RedisProvider { | ||
update: update, | ||
panic_on_cache_content_mismatch: panic_on_cache_content_mismatch, | ||
expire: expire, | ||
client: client | ||
} | ||
} | ||
|
||
fn get_key(&self, category: Option<&str>, key: &str) -> String { | ||
match category { | ||
Some(category) => format!("{}_{}", category, key), | ||
None => key.to_string() | ||
} | ||
} | ||
} | ||
|
||
impl CacheProvider for RedisProvider { | ||
fn get_entry(&self, category: Option<&str>, key: &str) -> io::Result<Vec<u8>> { | ||
let full_key = self.get_key(category, key); | ||
|
||
let mut con = self.client.get_connection().unwrap(); | ||
|
||
let ret: Result<Vec<u8>, RedisError>; | ||
if self.expire.is_some() { | ||
ret = con.get_ex(&full_key, Expiry::EX(self.expire.unwrap().try_into().unwrap())); | ||
} else { | ||
ret = con.get(&full_key); | ||
} | ||
|
||
match ret { | ||
Ok(data) => { | ||
// retrun err if data is empty | ||
if data.len() == 0 { | ||
Err(io::Error::new(io::ErrorKind::NotFound, "Not found")) | ||
} else { | ||
Ok(data) | ||
} | ||
}, | ||
Err(err) => Err(io::Error::new(io::ErrorKind::Other, err)) | ||
} | ||
} | ||
|
||
fn set_entry(&self, category: Option<&str>, key: &str, value: &Vec<u8>) { | ||
let full_key = self.get_key(category, key); | ||
let mut con = self.client.get_connection().unwrap(); | ||
if self.has_entry(category, key) { | ||
let _:() = con.expire(&full_key, self.expire.unwrap().into()).unwrap(); | ||
|
||
if self.panic_on_cache_content_mismatch && category != Some("obj") { | ||
let input_data = self.get_entry(category, key).expect(&format!("Unable to access redis key '{}'!", full_key)); | ||
if input_data != *value { | ||
panic!("content of '{}' does not match expected value! (hash collision?)", full_key); | ||
} | ||
} | ||
} else { | ||
if self.expire.is_some() { | ||
let _:() = con.set_ex(&full_key, value, self.expire.unwrap().into()).unwrap(); | ||
} else { | ||
let _:() = con.set(&full_key, value).unwrap(); | ||
} | ||
} | ||
} | ||
|
||
fn has_entry(&self, category: Option<&str>, key: &str) -> bool { | ||
let mut con = self.client.get_connection().unwrap(); | ||
|
||
con.exists(self.get_key(category, key)).unwrap() | ||
} | ||
|
||
fn update(&self) -> bool { | ||
self.update | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.