From 3c92b073c543788dcff79385766c739a5b7cc0c6 Mon Sep 17 00:00:00 2001 From: Akshay S Date: Tue, 23 Apr 2024 15:21:18 +0530 Subject: [PATCH 1/3] add profiles to in mem --- crates/router/src/db/business_profile.rs | 102 ++++++++++++++++++----- 1 file changed, 81 insertions(+), 21 deletions(-) diff --git a/crates/router/src/db/business_profile.rs b/crates/router/src/db/business_profile.rs index 4d5a94b370a6..62210fa6b0cd 100644 --- a/crates/router/src/db/business_profile.rs +++ b/crates/router/src/db/business_profile.rs @@ -8,6 +8,8 @@ use crate::{ db::MockDb, types::storage::{self, business_profile}, }; +#[cfg(feature = "accounts_cache")] +use storage_impl::redis::cache::{CacheKind, ACCOUNTS_CACHE}; #[async_trait::async_trait] pub trait BusinessProfileInterface { @@ -64,10 +66,20 @@ impl BusinessProfileInterface for Store { &self, profile_id: &str, ) -> CustomResult { - let conn = connection::pg_connection_read(self).await?; - storage::business_profile::BusinessProfile::find_by_profile_id(&conn, profile_id) - .await - .map_err(|error| report!(errors::StorageError::from(error))) + let db_func = || async { let conn = connection::pg_connection_read(self).await?; + storage::business_profile::BusinessProfile::find_by_profile_id(&conn, profile_id) + .await + .map_err(|error| report!(errors::StorageError::from(error))) + }; + #[cfg(not(feature = "accounts_cache"))] + { + db_func().await + } + #[cfg(feature = "accounts_cache")] + { + super::cache::get_or_populate_in_memory(self, profile_id, db_func, &ACCOUNTS_CACHE) + .await + } } #[instrument(skip_all)] @@ -76,14 +88,28 @@ impl BusinessProfileInterface for Store { profile_name: &str, merchant_id: &str, ) -> CustomResult { - let conn = connection::pg_connection_read(self).await?; - storage::business_profile::BusinessProfile::find_by_profile_name_merchant_id( - &conn, - profile_name, - merchant_id, - ) - .await - .map_err(|error| report!(errors::StorageError::from(error))) + let db_func = || async { + let conn = connection::pg_connection_read(self).await?; + storage::business_profile::BusinessProfile::find_by_profile_name_merchant_id( + &conn, + profile_name, + merchant_id, + ) + .await + .map_err(|error| report!(errors::StorageError::from(error))) + }; + + #[cfg(feature= "accounts_cache")] + { + let key = format!("{}_{}", profile_name ,merchant_id); + super::cache::get_or_populate_in_memory(self, &key, db_func, &ACCOUNTS_CACHE) + .await + } + + #[cfg(not(feature = "accounts_cache"))] + { + db_func().await + } } #[instrument(skip_all)] @@ -93,13 +119,19 @@ impl BusinessProfileInterface for Store { business_profile_update: business_profile::BusinessProfileUpdateInternal, ) -> CustomResult { let conn = connection::pg_connection_write(self).await?; - storage::business_profile::BusinessProfile::update_by_profile_id( + let updated_profile = storage::business_profile::BusinessProfile::update_by_profile_id( current_state, &conn, business_profile_update, ) .await - .map_err(|error| report!(errors::StorageError::from(error))) + .map_err(|error| report!(errors::StorageError::from(error)))?; + + #[cfg(feature = "accounts_cache")] + { + publish_and_redact_business_profile_cache(self, &updated_profile).await?; + } + Ok(updated_profile) } #[instrument(skip_all)] @@ -109,13 +141,28 @@ impl BusinessProfileInterface for Store { merchant_id: &str, ) -> CustomResult { let conn = connection::pg_connection_write(self).await?; - storage::business_profile::BusinessProfile::delete_by_profile_id_merchant_id( - &conn, - profile_id, - merchant_id, - ) - .await - .map_err(|error| report!(errors::StorageError::from(error))) + let db_func = || async { storage::business_profile::BusinessProfile::delete_by_profile_id_merchant_id( + &conn, + profile_id, + merchant_id, + ) + .await + .map_err(|error| report!(errors::StorageError::from(error))) + }; + + #[cfg(not(feature = "accounts_cache"))] + { + db_func().await + } + + #[cfg(feature = "accounts_cache")] + { + let business_profile = self.find_business_profile_by_profile_id(profile_id) + .await?; + let result: bool = db_func().await?; + publish_and_redact_business_profile_cache(self, &business_profile).await?; + Ok(result) + } } #[instrument(skip_all)] @@ -133,6 +180,19 @@ impl BusinessProfileInterface for Store { } } +#[cfg(feature = "accounts_cache")] +async fn publish_and_redact_business_profile_cache( + store: &dyn super::StorageInterface, + business_profile : &business_profile::BusinessProfile, +) -> CustomResult<(), errors::StorageError> { + let key1 = CacheKind::Accounts(business_profile.profile_id.as_str().into()); + let str_key = format!("{}_{}", business_profile.profile_name.as_str(), business_profile.merchant_id.as_str()); + let key2 = CacheKind::Accounts(str_key.as_str().into()); + let keys = vec![key1, key2]; + super::cache::publish_into_redact_channel(store, keys).await?; + Ok(()) +} + #[async_trait::async_trait] impl BusinessProfileInterface for MockDb { async fn insert_business_profile( From 839c645f221cfc1eaa64c7e1d0326668ae02f478 Mon Sep 17 00:00:00 2001 From: "hyperswitch-bot[bot]" <148525504+hyperswitch-bot[bot]@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:56:19 +0000 Subject: [PATCH 2/3] chore: run formatter --- crates/router/src/db/business_profile.rs | 62 +++++++++++++----------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/crates/router/src/db/business_profile.rs b/crates/router/src/db/business_profile.rs index 62210fa6b0cd..c1e1ceb81f31 100644 --- a/crates/router/src/db/business_profile.rs +++ b/crates/router/src/db/business_profile.rs @@ -1,5 +1,7 @@ use error_stack::report; use router_env::{instrument, tracing}; +#[cfg(feature = "accounts_cache")] +use storage_impl::redis::cache::{CacheKind, ACCOUNTS_CACHE}; use super::Store; use crate::{ @@ -8,8 +10,6 @@ use crate::{ db::MockDb, types::storage::{self, business_profile}, }; -#[cfg(feature = "accounts_cache")] -use storage_impl::redis::cache::{CacheKind, ACCOUNTS_CACHE}; #[async_trait::async_trait] pub trait BusinessProfileInterface { @@ -66,8 +66,9 @@ impl BusinessProfileInterface for Store { &self, profile_id: &str, ) -> CustomResult { - let db_func = || async { let conn = connection::pg_connection_read(self).await?; - storage::business_profile::BusinessProfile::find_by_profile_id(&conn, profile_id) + let db_func = || async { + let conn = connection::pg_connection_read(self).await?; + storage::business_profile::BusinessProfile::find_by_profile_id(&conn, profile_id) .await .map_err(|error| report!(errors::StorageError::from(error))) }; @@ -89,21 +90,20 @@ impl BusinessProfileInterface for Store { merchant_id: &str, ) -> CustomResult { let db_func = || async { - let conn = connection::pg_connection_read(self).await?; - storage::business_profile::BusinessProfile::find_by_profile_name_merchant_id( - &conn, - profile_name, - merchant_id, - ) - .await - .map_err(|error| report!(errors::StorageError::from(error))) + let conn = connection::pg_connection_read(self).await?; + storage::business_profile::BusinessProfile::find_by_profile_name_merchant_id( + &conn, + profile_name, + merchant_id, + ) + .await + .map_err(|error| report!(errors::StorageError::from(error))) }; - #[cfg(feature= "accounts_cache")] - { - let key = format!("{}_{}", profile_name ,merchant_id); - super::cache::get_or_populate_in_memory(self, &key, db_func, &ACCOUNTS_CACHE) - .await + #[cfg(feature = "accounts_cache")] + { + let key = format!("{}_{}", profile_name, merchant_id); + super::cache::get_or_populate_in_memory(self, &key, db_func, &ACCOUNTS_CACHE).await } #[cfg(not(feature = "accounts_cache"))] @@ -126,7 +126,7 @@ impl BusinessProfileInterface for Store { ) .await .map_err(|error| report!(errors::StorageError::from(error)))?; - + #[cfg(feature = "accounts_cache")] { publish_and_redact_business_profile_cache(self, &updated_profile).await?; @@ -141,13 +141,14 @@ impl BusinessProfileInterface for Store { merchant_id: &str, ) -> CustomResult { let conn = connection::pg_connection_write(self).await?; - let db_func = || async { storage::business_profile::BusinessProfile::delete_by_profile_id_merchant_id( - &conn, - profile_id, - merchant_id, - ) - .await - .map_err(|error| report!(errors::StorageError::from(error))) + let db_func = || async { + storage::business_profile::BusinessProfile::delete_by_profile_id_merchant_id( + &conn, + profile_id, + merchant_id, + ) + .await + .map_err(|error| report!(errors::StorageError::from(error))) }; #[cfg(not(feature = "accounts_cache"))] @@ -157,8 +158,7 @@ impl BusinessProfileInterface for Store { #[cfg(feature = "accounts_cache")] { - let business_profile = self.find_business_profile_by_profile_id(profile_id) - .await?; + let business_profile = self.find_business_profile_by_profile_id(profile_id).await?; let result: bool = db_func().await?; publish_and_redact_business_profile_cache(self, &business_profile).await?; Ok(result) @@ -183,10 +183,14 @@ impl BusinessProfileInterface for Store { #[cfg(feature = "accounts_cache")] async fn publish_and_redact_business_profile_cache( store: &dyn super::StorageInterface, - business_profile : &business_profile::BusinessProfile, + business_profile: &business_profile::BusinessProfile, ) -> CustomResult<(), errors::StorageError> { let key1 = CacheKind::Accounts(business_profile.profile_id.as_str().into()); - let str_key = format!("{}_{}", business_profile.profile_name.as_str(), business_profile.merchant_id.as_str()); + let str_key = format!( + "{}_{}", + business_profile.profile_name.as_str(), + business_profile.merchant_id.as_str() + ); let key2 = CacheKind::Accounts(str_key.as_str().into()); let keys = vec![key1, key2]; super::cache::publish_into_redact_channel(store, keys).await?; From 98b7c7acccb1962c3ccbc71258b87263c6d1210f Mon Sep 17 00:00:00 2001 From: Akshay S Date: Tue, 23 Apr 2024 15:55:42 +0530 Subject: [PATCH 3/3] resolved conflicts --- crates/router/src/db/business_profile.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/router/src/db/business_profile.rs b/crates/router/src/db/business_profile.rs index c1e1ceb81f31..17c0647ae2f3 100644 --- a/crates/router/src/db/business_profile.rs +++ b/crates/router/src/db/business_profile.rs @@ -159,9 +159,8 @@ impl BusinessProfileInterface for Store { #[cfg(feature = "accounts_cache")] { let business_profile = self.find_business_profile_by_profile_id(profile_id).await?; - let result: bool = db_func().await?; publish_and_redact_business_profile_cache(self, &business_profile).await?; - Ok(result) + db_func().await } }