feat(db): add fetch and update cache for admin table (#508)

This commit is contained in:
Kartikeya Hegde
2023-02-07 16:10:54 +05:30
committed by GitHub
parent 39524f95a9
commit b4da08666a
4 changed files with 83 additions and 21 deletions

View File

@ -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"]

View File

@ -1,5 +1,3 @@
#![allow(dead_code)]
use error_stack::ResultExt;
use super::Store;

View File

@ -66,11 +66,23 @@ impl MerchantAccountInterface for Store {
&self,
merchant_id: &str,
) -> CustomResult<storage::MerchantAccount, errors::StorageError> {
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<storage::MerchantAccount, errors::StorageError> {
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<storage::MerchantAccount, errors::StorageError> {
let update_func = || async {
let conn = pg_connection(&self.master_pool).await;
storage::MerchantAccount::update_with_specific_fields(&conn, merchant_id, merchant_account)
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<bool, errors::StorageError> {
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
}
}
}

View File

@ -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,