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(cache): add in-memory cache support for organization (#12480)

Sanchith Hegdeea92e95d2c03
diff --git a/crates/diesel_models/src/organization.rs b/crates/diesel_models/src/organization.rs
index 6004335ce7..42a0e1d852 100644
--- a/crates/diesel_models/src/organization.rs
+++ b/crates/diesel_models/src/organization.rs
@@ -1,4 +1,4 @@
-use common_utils::{id_type, pii};
+use common_utils::{custom_serde, id_type, pii};
use diesel::{AsChangeset, Identifiable, Insertable, Queryable, Selectable};
#[cfg(feature = "v1")]
@@ -11,7 +11,9 @@ pub trait OrganizationBridge {
fn set_organization_name(&mut self, organization_name: String);
}
#[cfg(feature = "v1")]
-#[derive(Clone, Debug, Identifiable, Queryable, Selectable)]
+#[derive(
+ Clone, Debug, Identifiable, Queryable, Selectable, serde::Serialize, serde::Deserialize,
+)]
#[diesel(
table_name = organization,
primary_key(org_id),
@@ -22,7 +24,9 @@ pub struct Organization {
org_name: Option<String>,
pub organization_details: Option<pii::SecretSerdeValue>,
pub metadata: Option<pii::SecretSerdeValue>,
+ #[serde(with = "custom_serde::iso8601")]
pub created_at: time::PrimitiveDateTime,
+ #[serde(with = "custom_serde::iso8601")]
pub modified_at: time::PrimitiveDateTime,
#[allow(dead_code)]
id: Option<id_type::OrganizationId>,
@@ -34,7 +38,9 @@ pub struct Organization {
}
#[cfg(feature = "v2")]
-#[derive(Clone, Debug, Identifiable, Queryable, Selectable)]
+#[derive(
+ Clone, Debug, Identifiable, Queryable, Selectable, serde::Serialize, serde::Deserialize,
+)]
#[diesel(
table_name = organization,
primary_key(id),
@@ -43,7 +49,9 @@ pub struct Organization {
pub struct Organization {
pub organization_details: Option<pii::SecretSerdeValue>,
pub metadata: Option<pii::SecretSerdeValue>,
+ #[serde(with = "custom_serde::iso8601")]
pub created_at: time::PrimitiveDateTime,
+ #[serde(with = "custom_serde::iso8601")]
pub modified_at: time::PrimitiveDateTime,
id: id_type::OrganizationId,
organization_name: Option<String>,
diff --git a/crates/router/src/db/organization.rs b/crates/router/src/db/organization.rs
index 06e4ee834d..7e606a5831 100644
--- a/crates/router/src/db/organization.rs
+++ b/crates/router/src/db/organization.rs
@@ -2,6 +2,8 @@ use common_utils::{errors::CustomResult, id_type};
use diesel_models::{organization as storage, organization::OrganizationBridge};
use error_stack::report;
use router_env::{instrument, tracing};
+#[cfg(feature = "accounts_cache")]
+use storage_impl::redis::cache::{self, CacheKind, ACCOUNTS_CACHE};
use crate::{connection, core::errors, services::Store};
@@ -43,10 +45,28 @@ impl OrganizationInterface for Store {
&self,
org_id: &id_type::OrganizationId,
) -> CustomResult<storage::Organization, errors::StorageError> {
- let conn = connection::pg_accounts_connection_read(self).await?;
- storage::Organization::find_by_org_id(&conn, org_id.to_owned())
+ let find_call = || async {
+ let conn = connection::pg_accounts_connection_read(self).await?;
+ storage::Organization::find_by_org_id(&conn, org_id.to_owned())
+ .await
+ .map_err(|error| report!(errors::StorageError::from(error)))
+ };
+
+ #[cfg(not(feature = "accounts_cache"))]
+ {
+ find_call().await
+ }
+
+ #[cfg(feature = "accounts_cache")]
+ {
+ cache::get_or_populate_in_memory(
+ self,
+ org_id.get_string_repr(),
+ find_call,
+ &ACCOUNTS_CACHE,
+ )
.await
- .map_err(|error| report!(errors::StorageError::from(error)))
+ }
}
#[instrument(skip_all)]
@@ -56,10 +76,26 @@ impl OrganizationInterface for Store {
update: storage::OrganizationUpdate,
) -> CustomResult<storage::Organization, errors::StorageError> {
let conn = connection::pg_accounts_connection_write(self).await?;
+ let update_call = || async {
+ storage::Organization::update_by_org_id(&conn, org_id.to_owned(), update)
+ .await
+ .map_err(|error| report!(errors::StorageError::from(error)))
+ };
- storage::Organization::update_by_org_id(&conn, org_id.to_owned(), update)
+ #[cfg(not(feature = "accounts_cache"))]
+ {
+ update_call().await
+ }
+
+ #[cfg(feature = "accounts_cache")]
+ {
+ cache::publish_and_redact(
+ self,
+ CacheKind::Accounts(org_id.get_string_repr().into()),
+ update_call,
+ )
.await
- .map_err(|error| report!(errors::StorageError::from(error)))
+ }
}
}