Skip to content

Commit eb10aca

Browse files
feat(db): enable caching for merchant_account fetch using publishable key (#2186)
1 parent e4b3cc7 commit eb10aca

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

crates/router/src/db/merchant_account.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,23 +198,44 @@ impl MerchantAccountInterface for Store {
198198
&self,
199199
publishable_key: &str,
200200
) -> CustomResult<authentication::AuthenticationData, errors::StorageError> {
201-
let conn = connection::pg_connection_read(self).await?;
202-
let merchant = storage::MerchantAccount::find_by_publishable_key(&conn, publishable_key)
203-
.await
204-
.map_err(Into::into)
205-
.into_report()?;
201+
let fetch_by_pub_key_func = || async {
202+
let conn = connection::pg_connection_read(self).await?;
203+
204+
storage::MerchantAccount::find_by_publishable_key(&conn, publishable_key)
205+
.await
206+
.map_err(Into::into)
207+
.into_report()
208+
};
209+
210+
let merchant_account;
211+
#[cfg(not(feature = "accounts_cache"))]
212+
{
213+
merchant_account = fetch_by_pub_key_func().await?;
214+
}
215+
216+
#[cfg(feature = "accounts_cache")]
217+
{
218+
merchant_account = super::cache::get_or_populate_in_memory(
219+
self,
220+
publishable_key,
221+
fetch_by_pub_key_func,
222+
&ACCOUNTS_CACHE,
223+
)
224+
.await?;
225+
}
206226
let key_store = self
207227
.get_merchant_key_store_by_merchant_id(
208-
&merchant.merchant_id,
228+
&merchant_account.merchant_id,
209229
&self.get_master_key().to_vec().into(),
210230
)
211231
.await?;
212232

213233
Ok(authentication::AuthenticationData {
214-
merchant_account: merchant
234+
merchant_account: merchant_account
215235
.convert(key_store.key.get_inner())
216236
.await
217237
.change_context(errors::StorageError::DecryptionError)?,
238+
218239
key_store,
219240
})
220241
}

0 commit comments

Comments
 (0)