implemented working cache
some testing still has to be done
This commit is contained in:
@@ -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(())
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user