create caching library
This commit is contained in:
2
backend/cache/.gitignore
vendored
Normal file
2
backend/cache/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
target/
|
||||
.env
|
1014
backend/cache/Cargo.lock
generated
vendored
Normal file
1014
backend/cache/Cargo.lock
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
9
backend/cache/Cargo.toml
vendored
Normal file
9
backend/cache/Cargo.toml
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "cache"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
fred = "10.1.0"
|
||||
serde = "1.0.219"
|
||||
serde_json = "1.0.140"
|
70
backend/cache/src/lib.rs
vendored
Normal file
70
backend/cache/src/lib.rs
vendored
Normal file
@@ -0,0 +1,70 @@
|
||||
pub use fred::{
|
||||
clients::Pool,
|
||||
interfaces::KeysInterface,
|
||||
prelude::*,
|
||||
types::{Expiration, SetOptions},
|
||||
};
|
||||
|
||||
pub struct Cache {
|
||||
pub inmem: Pool,
|
||||
}
|
||||
|
||||
impl Cache {
|
||||
pub async fn get<T>(&mut self, key: String) -> Result<Option<T>, Box<dyn std::error::Error>>
|
||||
where
|
||||
T: for<'de> serde::Deserialize<'de>,
|
||||
{
|
||||
self.is_connected()?;
|
||||
let value: Option<String> = self.inmem.get(&key).await?;
|
||||
|
||||
match value {
|
||||
Some(json_str) => match serde_json::from_str::<T>(&json_str) {
|
||||
Ok(deserialized) => Ok(Some(deserialized)),
|
||||
Err(_) => Ok(None),
|
||||
},
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn set<T>(
|
||||
&mut self,
|
||||
key: String,
|
||||
contents: &T,
|
||||
expiration: Option<Expiration>,
|
||||
set_opts: Option<SetOptions>,
|
||||
get: bool,
|
||||
) -> Result<(), Box<dyn std::error::Error>>
|
||||
where
|
||||
T: for<'de> serde::Deserialize<'de> + serde::Serialize,
|
||||
{
|
||||
self.is_connected()?;
|
||||
let json_string = match serde_json::to_string::<T>(contents) {
|
||||
Ok(s) => s,
|
||||
Err(_) => {
|
||||
return Err(Box::new(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
"Unable to deserialize contents passed to cache".to_string(),
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
Ok(self
|
||||
.inmem
|
||||
.set(key, json_string, expiration, set_opts, get)
|
||||
.await?)
|
||||
}
|
||||
|
||||
pub async fn del(&mut self, key: String) -> Result<(), Box<dyn std::error::Error>> {
|
||||
Ok(self.inmem.del(key).await?)
|
||||
}
|
||||
|
||||
fn is_connected(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
match self.inmem.is_connected() {
|
||||
true => Ok(()),
|
||||
false => Err(Box::new(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
"Not connected to cache".to_string(),
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user