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(db): enable caching for merchant_account fetch using publishable key (#2186)

Abhishek Marrivagueb10aca6313b
diff --git a/crates/router/src/db/merchant_account.rs b/crates/router/src/db/merchant_account.rs
index d8bbd911de..0699c7d8ac 100644
--- a/crates/router/src/db/merchant_account.rs
+++ b/crates/router/src/db/merchant_account.rs
@@ -198,23 +198,44 @@ impl MerchantAccountInterface for Store {
&self,
publishable_key: &str,
) -> CustomResult<authentication::AuthenticationData, errors::StorageError> {
- let conn = connection::pg_connection_read(self).await?;
- let merchant = storage::MerchantAccount::find_by_publishable_key(&conn, publishable_key)
- .await
- .map_err(Into::into)
- .into_report()?;
+ let fetch_by_pub_key_func = || async {
+ let conn = connection::pg_connection_read(self).await?;
+
+ storage::MerchantAccount::find_by_publishable_key(&conn, publishable_key)
+ .await
+ .map_err(Into::into)
+ .into_report()
+ };
+
+ let merchant_account;
+ #[cfg(not(feature = "accounts_cache"))]
+ {
+ merchant_account = fetch_by_pub_key_func().await?;
+ }
+
+ #[cfg(feature = "accounts_cache")]
+ {
+ merchant_account = super::cache::get_or_populate_in_memory(
+ self,
+ publishable_key,
+ fetch_by_pub_key_func,
+ &ACCOUNTS_CACHE,
+ )
+ .await?;
+ }
let key_store = self
.get_merchant_key_store_by_merchant_id(
- &merchant.merchant_id,
+ &merchant_account.merchant_id,
&self.get_master_key().to_vec().into(),
)
.await?;
Ok(authentication::AuthenticationData {
- merchant_account: merchant
+ merchant_account: merchant_account
.convert(key_store.key.get_inner())
.await
.change_context(errors::StorageError::DecryptionError)?,
+
key_store,
})
}