feat(cache): add generic cache functions to populate and invalidate cache (#483)

This commit is contained in:
Kartikeya Hegde
2023-02-03 19:35:41 +05:30
committed by GitHub
parent 4d6031f7e4
commit 888fb2bcce
2 changed files with 58 additions and 0 deletions

View File

@ -1,4 +1,5 @@
pub mod address;
pub mod cache;
pub mod configs;
pub mod connector_response;
pub mod customers;

View File

@ -0,0 +1,57 @@
#![allow(dead_code)]
use error_stack::ResultExt;
use super::Store;
use crate::core::errors::{self, CustomResult};
pub async fn get_or_populate_cache<T, F, Fut>(
store: &Store,
key: &str,
fun: F,
) -> CustomResult<T, errors::StorageError>
where
T: serde::Serialize + serde::de::DeserializeOwned + std::fmt::Debug,
F: FnOnce() -> Fut + Send,
Fut: futures::Future<Output = CustomResult<T, errors::StorageError>> + Send,
{
let redis = &store.redis_conn;
let redis_val = redis
.get_and_deserialize_key::<T>(key, std::any::type_name::<T>())
.await;
match redis_val {
Err(err) => match err.current_context() {
errors::RedisError::NotFound => {
let data = fun().await?;
redis
.serialize_and_set_key(key, &data)
.await
.change_context(errors::StorageError::KVError)?;
Ok(data)
}
_ => Err(err
.change_context(errors::StorageError::KVError)
.attach_printable("Error while fetching config")),
},
Ok(val) => Ok(val),
}
}
pub async fn redact_cache<T, F, Fut>(
store: &Store,
key: &str,
fun: F,
) -> CustomResult<T, errors::StorageError>
where
T: serde::Serialize + serde::de::DeserializeOwned + std::fmt::Debug,
F: FnOnce() -> Fut + Send,
Fut: futures::Future<Output = CustomResult<T, errors::StorageError>> + Send,
{
let data = fun().await?;
store
.redis_conn
.delete_key(key)
.await
.change_context(errors::StorageError::KVError)?;
Ok(data)
}