Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

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

P K Navin Shrinivasfda3fb4d2bc6
diff --git a/crates/router/src/db/merchant_key_store.rs b/crates/router/src/db/merchant_key_store.rs
index b4637af8c0..d560e7cda3 100644
--- a/crates/router/src/db/merchant_key_store.rs
+++ b/crates/router/src/db/merchant_key_store.rs
@@ -1,5 +1,7 @@
use error_stack::{IntoReport, ResultExt};
+#[cfg(feature = "accounts_cache")]
+use crate::cache::ACCOUNTS_CACHE;
use crate::{
connection,
core::errors::{self, CustomResult},
@@ -48,17 +50,40 @@ impl MerchantKeyStoreInterface for Store {
&self,
merchant_id: &str,
) -> CustomResult<merchant_key_store::MerchantKeyStore, errors::StorageError> {
- let conn = connection::pg_connection_read(self).await?;
- storage_models::merchant_key_store::MerchantKeyStore::find_by_merchant_id(
- &conn,
- merchant_id,
- )
- .await
- .map_err(Into::into)
- .into_report()?
- .convert(self, merchant_id)
- .await
- .change_context(errors::StorageError::DecryptionError)
+ let fetch_func = || async {
+ let conn = connection::pg_connection_read(self).await?;
+
+ storage_models::merchant_key_store::MerchantKeyStore::find_by_merchant_id(
+ &conn,
+ merchant_id,
+ )
+ .await
+ .map_err(Into::into)
+ .into_report()
+ };
+ #[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)
+ }
}
}