chore: use generic cache functions for config table (#506)

This commit is contained in:
Kartikeya Hegde
2023-02-07 15:26:28 +05:30
committed by GitHub
parent 7666748922
commit 3b7f59158d
2 changed files with 11 additions and 33 deletions

View File

@ -15,10 +15,9 @@ where
F: FnOnce() -> Fut + Send,
Fut: futures::Future<Output = CustomResult<T, errors::StorageError>> + Send,
{
let type_name = std::any::type_name::<T>();
let redis = &store.redis_conn;
let redis_val = redis
.get_and_deserialize_key::<T>(key, std::any::type_name::<T>())
.await;
let redis_val = redis.get_and_deserialize_key::<T>(key, type_name).await;
match redis_val {
Err(err) => match err.current_context() {
errors::RedisError::NotFound => {
@ -31,7 +30,7 @@ where
}
_ => Err(err
.change_context(errors::StorageError::KVError)
.attach_printable("Error while fetching config")),
.attach_printable(format!("Error while fetching cache for {type_name}"))),
},
Ok(val) => Ok(val),
}

View File

@ -1,6 +1,6 @@
use error_stack::{IntoReport, ResultExt};
use error_stack::IntoReport;
use super::{MockDb, Store};
use super::{cache, MockDb, Store};
use crate::{
connection::pg_connection,
core::errors::{self, CustomResult},
@ -76,39 +76,18 @@ impl ConfigInterface for Store {
key: &str,
config_update: storage::ConfigUpdate,
) -> CustomResult<storage::Config, errors::StorageError> {
let config = self.update_config_by_key(key, config_update).await?;
self.redis_conn
.delete_key(key)
.await
.change_context(errors::StorageError::KVError)
.attach_printable("Error while deleting the config key")?;
Ok(config)
cache::redact_cache(self, key, || async {
self.update_config_by_key(key, config_update).await
})
.await
}
async fn find_config_by_key_cached(
&self,
key: &str,
) -> CustomResult<storage::Config, errors::StorageError> {
let redis = &self.redis_conn;
let redis_val = redis
.get_and_deserialize_key::<storage::Config>(key, "Config")
.await;
Ok(match redis_val {
Err(err) => match err.current_context() {
errors::RedisError::NotFound => {
let config = self.find_config_by_key(key).await?;
redis
.serialize_and_set_key(&config.key, &config)
.await
.change_context(errors::StorageError::KVError)?;
config
}
_ => Err(err
.change_context(errors::StorageError::KVError)
.attach_printable("Error while fetching config"))?,
},
Ok(val) => val,
})
cache::get_or_populate_cache(self, key, || async { self.find_config_by_key(key).await })
.await
}
async fn delete_config_by_key(&self, key: &str) -> CustomResult<bool, errors::StorageError> {