feat(router): add caching for MerchantKeyStore (#1409)

This commit is contained in:
P K Navin Shrinivas
2023-06-14 15:30:44 +05:30
committed by GitHub
parent 6563587564
commit fda3fb4d2b

View File

@ -1,5 +1,7 @@
use error_stack::{IntoReport, ResultExt}; use error_stack::{IntoReport, ResultExt};
#[cfg(feature = "accounts_cache")]
use crate::cache::ACCOUNTS_CACHE;
use crate::{ use crate::{
connection, connection,
core::errors::{self, CustomResult}, core::errors::{self, CustomResult},
@ -48,17 +50,40 @@ impl MerchantKeyStoreInterface for Store {
&self, &self,
merchant_id: &str, merchant_id: &str,
) -> CustomResult<merchant_key_store::MerchantKeyStore, errors::StorageError> { ) -> CustomResult<merchant_key_store::MerchantKeyStore, errors::StorageError> {
let conn = connection::pg_connection_read(self).await?; let fetch_func = || async {
storage_models::merchant_key_store::MerchantKeyStore::find_by_merchant_id( let conn = connection::pg_connection_read(self).await?;
&conn,
merchant_id, storage_models::merchant_key_store::MerchantKeyStore::find_by_merchant_id(
) &conn,
.await merchant_id,
.map_err(Into::into) )
.into_report()? .await
.convert(self, merchant_id) .map_err(Into::into)
.await .into_report()
.change_context(errors::StorageError::DecryptionError) };
#[cfg(not(feature = "accounts_cache"))]
{
fetch_func()
.await?
.convert(self, merchant_id)
.await
.change_context(errors::StorageError::DecryptionError)
}
#[cfg(feature = "accounts_cache")]
{
let key_store_cache_key = format!("merchant_key_store_{}", merchant_id);
super::cache::get_or_populate_in_memory(
self,
&key_store_cache_key,
fetch_func,
&ACCOUNTS_CACHE,
)
.await?
.convert(self, merchant_id)
.await
.change_context(errors::StorageError::DecryptionError)
}
} }
} }