mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-02 21:07:58 +08:00
chore: use generic cache functions for config table (#506)
This commit is contained in:
@ -15,10 +15,9 @@ where
|
|||||||
F: FnOnce() -> Fut + Send,
|
F: FnOnce() -> Fut + Send,
|
||||||
Fut: futures::Future<Output = CustomResult<T, errors::StorageError>> + 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 = &store.redis_conn;
|
||||||
let redis_val = redis
|
let redis_val = redis.get_and_deserialize_key::<T>(key, type_name).await;
|
||||||
.get_and_deserialize_key::<T>(key, std::any::type_name::<T>())
|
|
||||||
.await;
|
|
||||||
match redis_val {
|
match redis_val {
|
||||||
Err(err) => match err.current_context() {
|
Err(err) => match err.current_context() {
|
||||||
errors::RedisError::NotFound => {
|
errors::RedisError::NotFound => {
|
||||||
@ -31,7 +30,7 @@ where
|
|||||||
}
|
}
|
||||||
_ => Err(err
|
_ => Err(err
|
||||||
.change_context(errors::StorageError::KVError)
|
.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),
|
Ok(val) => Ok(val),
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
use error_stack::{IntoReport, ResultExt};
|
use error_stack::IntoReport;
|
||||||
|
|
||||||
use super::{MockDb, Store};
|
use super::{cache, MockDb, Store};
|
||||||
use crate::{
|
use crate::{
|
||||||
connection::pg_connection,
|
connection::pg_connection,
|
||||||
core::errors::{self, CustomResult},
|
core::errors::{self, CustomResult},
|
||||||
@ -76,39 +76,18 @@ impl ConfigInterface for Store {
|
|||||||
key: &str,
|
key: &str,
|
||||||
config_update: storage::ConfigUpdate,
|
config_update: storage::ConfigUpdate,
|
||||||
) -> CustomResult<storage::Config, errors::StorageError> {
|
) -> CustomResult<storage::Config, errors::StorageError> {
|
||||||
let config = self.update_config_by_key(key, config_update).await?;
|
cache::redact_cache(self, key, || async {
|
||||||
self.redis_conn
|
self.update_config_by_key(key, config_update).await
|
||||||
.delete_key(key)
|
})
|
||||||
.await
|
.await
|
||||||
.change_context(errors::StorageError::KVError)
|
|
||||||
.attach_printable("Error while deleting the config key")?;
|
|
||||||
Ok(config)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn find_config_by_key_cached(
|
async fn find_config_by_key_cached(
|
||||||
&self,
|
&self,
|
||||||
key: &str,
|
key: &str,
|
||||||
) -> CustomResult<storage::Config, errors::StorageError> {
|
) -> CustomResult<storage::Config, errors::StorageError> {
|
||||||
let redis = &self.redis_conn;
|
cache::get_or_populate_cache(self, key, || async { self.find_config_by_key(key).await })
|
||||||
let redis_val = redis
|
.await
|
||||||
.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,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn delete_config_by_key(&self, key: &str) -> CustomResult<bool, errors::StorageError> {
|
async fn delete_config_by_key(&self, key: &str) -> CustomResult<bool, errors::StorageError> {
|
||||||
|
|||||||
Reference in New Issue
Block a user