diff --git a/crates/router/Cargo.toml b/crates/router/Cargo.toml index 7bf42027bf..0e597e91e6 100644 --- a/crates/router/Cargo.toml +++ b/crates/router/Cargo.toml @@ -10,7 +10,7 @@ license = "Apache-2.0" build = "src/build.rs" [features] -default = ["kv_store", "stripe", "oltp", "olap"] +default = ["kv_store", "stripe", "oltp", "olap","accounts_cache"] kms = ["aws-config", "aws-sdk-kms"] basilisk = [] stripe = ["dep:serde_qs"] @@ -19,6 +19,7 @@ olap = [] oltp = [] production = [] kv_store = [] +accounts_cache = [] openapi = ["olap", "oltp"] diff --git a/crates/router/src/db/cache.rs b/crates/router/src/db/cache.rs index 33f4141182..29e7ed4da2 100644 --- a/crates/router/src/db/cache.rs +++ b/crates/router/src/db/cache.rs @@ -1,5 +1,3 @@ -#![allow(dead_code)] - use error_stack::ResultExt; use super::Store; diff --git a/crates/router/src/db/merchant_account.rs b/crates/router/src/db/merchant_account.rs index 3aac43b6e4..3bc7589b86 100644 --- a/crates/router/src/db/merchant_account.rs +++ b/crates/router/src/db/merchant_account.rs @@ -66,11 +66,23 @@ impl MerchantAccountInterface for Store { &self, merchant_id: &str, ) -> CustomResult { - let conn = pg_connection(&self.master_pool).await; - storage::MerchantAccount::find_by_merchant_id(&conn, merchant_id) - .await - .map_err(Into::into) - .into_report() + let fetch_func = || async { + let conn = pg_connection(&self.master_pool).await; + storage::MerchantAccount::find_by_merchant_id(&conn, merchant_id) + .await + .map_err(Into::into) + .into_report() + }; + + #[cfg(not(feature = "accounts_cache"))] + { + fetch_func().await + } + + #[cfg(feature = "accounts_cache")] + { + super::cache::get_or_populate_cache(self, merchant_id, fetch_func).await + } } async fn update_merchant( @@ -78,11 +90,24 @@ impl MerchantAccountInterface for Store { this: storage::MerchantAccount, merchant_account: storage::MerchantAccountUpdate, ) -> CustomResult { - let conn = pg_connection(&self.master_pool).await; - this.update(&conn, merchant_account) - .await - .map_err(Into::into) - .into_report() + let _merchant_id = this.merchant_id.clone(); + let update_func = || async { + let conn = pg_connection(&self.master_pool).await; + this.update(&conn, merchant_account) + .await + .map_err(Into::into) + .into_report() + }; + + #[cfg(not(feature = "accounts_cache"))] + { + update_func().await + } + + #[cfg(feature = "accounts_cache")] + { + super::cache::redact_cache(self, &_merchant_id, update_func).await + } } async fn update_specific_fields_in_merchant( @@ -90,11 +115,27 @@ impl MerchantAccountInterface for Store { merchant_id: &str, merchant_account: storage::MerchantAccountUpdate, ) -> CustomResult { - let conn = pg_connection(&self.master_pool).await; - storage::MerchantAccount::update_with_specific_fields(&conn, merchant_id, merchant_account) + let update_func = || async { + let conn = pg_connection(&self.master_pool).await; + storage::MerchantAccount::update_with_specific_fields( + &conn, + merchant_id, + merchant_account, + ) .await .map_err(Into::into) .into_report() + }; + + #[cfg(not(feature = "accounts_cache"))] + { + update_func().await + } + + #[cfg(feature = "accounts_cache")] + { + super::cache::redact_cache(self, merchant_id, update_func).await + } } async fn find_merchant_account_by_api_key( @@ -123,11 +164,23 @@ impl MerchantAccountInterface for Store { &self, merchant_id: &str, ) -> CustomResult { - let conn = pg_connection(&self.master_pool).await; - storage::MerchantAccount::delete_by_merchant_id(&conn, merchant_id) - .await - .map_err(Into::into) - .into_report() + let delete_func = || async { + let conn = pg_connection(&self.master_pool).await; + storage::MerchantAccount::delete_by_merchant_id(&conn, merchant_id) + .await + .map_err(Into::into) + .into_report() + }; + + #[cfg(not(feature = "accounts_cache"))] + { + delete_func().await + } + + #[cfg(feature = "accounts_cache")] + { + super::cache::redact_cache(self, merchant_id, delete_func).await + } } } diff --git a/crates/storage_models/src/merchant_account.rs b/crates/storage_models/src/merchant_account.rs index 5147b1077a..38f8a5bae5 100644 --- a/crates/storage_models/src/merchant_account.rs +++ b/crates/storage_models/src/merchant_account.rs @@ -3,7 +3,17 @@ use masking::StrongSecret; use crate::{enums as storage_enums, schema::merchant_account}; -#[derive(Clone, Debug, Eq, PartialEq, Identifiable, Queryable, router_derive::DebugAsDisplay)] +#[derive( + Clone, + Debug, + serde::Deserialize, + serde::Serialize, + Eq, + PartialEq, + Identifiable, + Queryable, + router_derive::DebugAsDisplay, +)] #[diesel(table_name = merchant_account)] pub struct MerchantAccount { pub id: i32,