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

fix(core): Store encrypted profile in application cache (#13580)

Anurag Thakur77c45d76b2b7
diff --git a/crates/storage_impl/src/business_profile.rs b/crates/storage_impl/src/business_profile.rs
index 5e7432dfb5..ab8f3c519f 100644
--- a/crates/storage_impl/src/business_profile.rs
+++ b/crates/storage_impl/src/business_profile.rs
@@ -25,13 +25,6 @@ use crate::{
#[cfg(feature = "accounts_cache")]
use crate::{metrics, RedisConnInterface};
-/// Time to live for the redis copy of a profile, in seconds.
-///
-/// Tuned independently of the in-memory cache's TTL, and deliberately longer than it, so that an
-/// expired in-memory entry can be refilled from redis instead of the database.
-#[cfg(feature = "accounts_cache")]
-const PROFILE_REDIS_CACHE_TTL: i64 = 60 * 60;
-
/// Cache key for a profile, shared by both the profile-scoped and merchant-scoped lookups.
#[cfg(feature = "accounts_cache")]
fn profile_cache_key(profile_id: &common_utils::id_type::ProfileId) -> String {
@@ -201,12 +194,12 @@ impl<T: DatabaseStore> ProfileInterface for RouterStore<T> {
.await
.map_err(|error| report!(StorageError::from(error)))
};
+ let state = self
+ .get_keymanager_state()
+ .attach_printable("Missing KeyManagerState")?;
#[cfg(not(feature = "accounts_cache"))]
{
- let state = self
- .get_keymanager_state()
- .attach_printable("Missing KeyManagerState")?;
fetch_func()
.await?
.convert(
@@ -220,27 +213,20 @@ impl<T: DatabaseStore> ProfileInterface for RouterStore<T> {
#[cfg(feature = "accounts_cache")]
{
- let state = self
- .get_keymanager_state()
- .attach_printable("Missing KeyManagerState")?
- .clone();
-
- let identifier = merchant_key_store.merchant_id.clone().into();
-
- cache::get_or_populate_in_memory_with_transform(
+ cache::get_or_populate_in_memory(
self,
&profile_cache_key(profile_id),
- Some(PROFILE_REDIS_CACHE_TTL),
fetch_func,
- |diesel_profile| async move {
- diesel_profile
- .convert(&state, &merchant_key_store.key, identifier)
- .await
- .change_context(StorageError::DecryptionError)
- },
&ACCOUNTS_CACHE,
)
+ .await?
+ .convert(
+ state,
+ merchant_key_store.key.get_inner(),
+ merchant_key_store.merchant_id.clone().into(),
+ )
.await
+ .change_context(StorageError::DecryptionError)
}
}
@@ -256,12 +242,12 @@ impl<T: DatabaseStore> ProfileInterface for RouterStore<T> {
.await
.map_err(|error| report!(StorageError::from(error)))
};
+ let state = self
+ .get_keymanager_state()
+ .attach_printable("Missing KeyManagerState")?;
#[cfg(not(feature = "accounts_cache"))]
{
- let state = self
- .get_keymanager_state()
- .attach_printable("Missing KeyManagerState")?;
fetch_func()
.await?
.convert(
@@ -275,27 +261,20 @@ impl<T: DatabaseStore> ProfileInterface for RouterStore<T> {
#[cfg(feature = "accounts_cache")]
{
- let state = self
- .get_keymanager_state()
- .attach_printable("Missing KeyManagerState")?
- .clone();
-
- let identifier = merchant_key_store.merchant_id.clone().into();
-
- cache::get_or_populate_in_memory_with_transform(
+ cache::get_or_populate_in_memory(
self,
&profile_cache_key(profile_id),
- Some(PROFILE_REDIS_CACHE_TTL),
fetch_func,
- |diesel_profile| async move {
- diesel_profile
- .convert(&state, &merchant_key_store.key, identifier)
- .await
- .change_context(StorageError::DecryptionError)
- },
&ACCOUNTS_CACHE,
)
+ .await?
+ .convert(
+ state,
+ merchant_key_store.key.get_inner(),
+ merchant_key_store.merchant_id.clone().into(),
+ )
.await
+ .change_context(StorageError::DecryptionError)
}
}
diff --git a/crates/storage_impl/src/redis/cache.rs b/crates/storage_impl/src/redis/cache.rs
index 70eaae5ec4..9963cb23a8 100644
--- a/crates/storage_impl/src/redis/cache.rs
+++ b/crates/storage_impl/src/redis/cache.rs
@@ -454,67 +454,6 @@ where
}
}
-/// Two-tier cache where redis (L2) holds type `R` — typically the encrypted diesel model — and
-/// the in-memory cache (L1) holds type `T` — typically the decrypted domain model.
-/// `transform_func` converts the redis representation into the in-memory one, so that the
-/// (potentially expensive) decryption is paid once per in-memory population rather than once per
-/// read.
-///
-/// `redis_ttl` is the time to live for the redis entry in seconds; `None` stores it without an
-/// expiry. It is deliberately independent of the in-memory cache's TTL so the two tiers can be
-/// tuned separately — set it longer than the in-memory TTL to let an expired in-memory entry be
-/// refilled from redis instead of the database.
-#[instrument(skip_all)]
-pub async fn get_or_populate_in_memory_with_transform<T, R, F, Fut, TransF, TransFut>(
- store: &(dyn RedisConnInterface + Send + Sync),
- key: &str,
- redis_ttl: Option<i64>,
- fetch_func: F,
- transform_func: TransF,
- cache: &Cache,
-) -> CustomResult<T, StorageError>
-where
- T: Cacheable + Debug + Clone,
- R: serde::Serialize + serde::de::DeserializeOwned + Debug + Send,
- F: FnOnce() -> Fut + Send,
- Fut: futures::Future<Output = CustomResult<R, StorageError>> + Send,
- TransF: FnOnce(R) -> TransFut + Send,
- TransFut: futures::Future<Output = CustomResult<T, StorageError>> + Send,
-{
- let redis = &store
- .get_redis_conn()
- .change_context(StorageError::RedisError(
- RedisError::RedisConnectionError.into(),
- ))
- .attach_printable("Failed to get redis connection")?;
-
- let cache_val = cache
- .get_val::<T>(CacheKey {
- key: key.to_string(),
- prefix: redis.redis_conn.key_prefix.clone(),
- })
- .await;
- if let Some(val) = cache_val {
- return Ok(val);
- }
-
- let redis_model = get_or_populate_redis(redis, key, redis_ttl, fetch_func).await?;
-
- let domain_model = transform_func(redis_model).await?;
-
- cache
- .push(
- CacheKey {
- key: key.to_string(),
- prefix: redis.redis_conn.key_prefix.clone(),
- },
- domain_model.clone(),
- )
- .await;
-
- Ok(domain_model)
-}
-
#[instrument(skip_all)]
pub async fn redact_from_redis_and_publish<
'a,