implemented working cache

some testing still has to be done
This commit is contained in:
2025-03-16 02:56:54 -04:00
parent 328dacb675
commit d126fed2bd
5 changed files with 457 additions and 71 deletions

View File

@@ -29,19 +29,21 @@ impl Cache {
T: for<'de> serde::Deserialize<'de>,
{
if !self.inmem.is_connected() {
return Err(Box::new("Are you connected to the cache?".into()));
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
"Not connected to cache".to_string(),
)));
}
let value: Option<Value> = self.inmem.get(key).await?;
let value: Option<String> = self.inmem.get(&key).await?;
let result = match value {
Some(x) => match serde_json::from_value(x) {
Ok(x) => Some(x),
Err(_) => None,
match value {
Some(json_str) => match serde_json::from_str::<T>(&json_str) {
Ok(deserialized) => Ok(Some(deserialized)),
Err(_) => Ok(None),
},
None => None,
};
Ok(result)
None => Ok(None),
}
}
pub async fn set<T>(
@@ -53,16 +55,20 @@ impl Cache {
get: bool,
) -> Result<(), Box<dyn std::error::Error>>
where
T: for<'de> serde::Deserialize<'de>,
T: for<'de> serde::Deserialize<'de> + serde::Serialize,
{
if !self.inmem.is_connected() {
return Err(Box::new("Are you connected to the cache?".into()));
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::Other,
"Not connected to cache".to_string(),
)));
}
let value: Value = serde_json::to_value(contents)?;
let json_string = serde_json::to_string(contents)?;
self.inmem
.set(key, value.to_string(), expiration, set_opts, get)
.set(key, json_string, expiration, set_opts, get)
.await?;
Ok(())
}