feat(routing): Use Moka cache for routing with cache invalidation (#3216)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
Sarthak Soni
2024-05-22 14:02:50 +05:30
committed by GitHub
parent 8fa2cd556b
commit 431560b7fb
9 changed files with 134 additions and 122 deletions

View File

@ -10,10 +10,11 @@ use diesel_models::{
};
use error_stack::ResultExt;
use rustc_hash::FxHashSet;
use storage_impl::redis::cache as redis_cache;
use crate::{
core::errors::{self, RouterResult},
db::StorageInterface,
db::{cache, StorageInterface},
types::{domain, storage},
utils::StringExt,
};
@ -239,6 +240,17 @@ pub async fn update_business_profile_active_algorithm_ref(
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to convert routing ref to value")?;
let merchant_id = current_business_profile.merchant_id.clone();
#[cfg(feature = "business_profile_routing")]
let profile_id = current_business_profile.profile_id.clone();
#[cfg(feature = "business_profile_routing")]
let routing_cache_key = redis_cache::CacheKind::Routing(
format!("routing_config_{merchant_id}_{profile_id}").into(),
);
#[cfg(not(feature = "business_profile_routing"))]
let routing_cache_key = redis_cache::CacheKind::Routing(format!("dsl_{merchant_id}").into());
let (routing_algorithm, payout_routing_algorithm) = match transaction_type {
storage::enums::TransactionType::Payment => (Some(ref_val), None),
#[cfg(feature = "payouts")]
@ -272,6 +284,11 @@ pub async fn update_business_profile_active_algorithm_ref(
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to update routing algorithm ref in business profile")?;
cache::publish_into_redact_channel(db, [routing_cache_key])
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to invalidate routing cache")?;
Ok(())
}