mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-29 00:49:42 +08:00
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:
@ -21,6 +21,12 @@ const CONFIG_CACHE_PREFIX: &str = "config";
|
||||
/// Prefix for accounts cache key
|
||||
const ACCOUNTS_CACHE_PREFIX: &str = "accounts";
|
||||
|
||||
/// Prefix for routing cache key
|
||||
const ROUTING_CACHE_PREFIX: &str = "routing";
|
||||
|
||||
/// Prefix for kgraph cache key
|
||||
const CGRAPH_CACHE_PREFIX: &str = "cgraph";
|
||||
|
||||
/// Prefix for all kinds of cache key
|
||||
const ALL_CACHE_PREFIX: &str = "all_cache_kind";
|
||||
|
||||
@ -40,6 +46,14 @@ pub static CONFIG_CACHE: Lazy<Cache> = Lazy::new(|| Cache::new(CACHE_TTL, CACHE_
|
||||
pub static ACCOUNTS_CACHE: Lazy<Cache> =
|
||||
Lazy::new(|| Cache::new(CACHE_TTL, CACHE_TTI, Some(MAX_CAPACITY)));
|
||||
|
||||
/// Routing Cache
|
||||
pub static ROUTING_CACHE: Lazy<Cache> =
|
||||
Lazy::new(|| Cache::new(CACHE_TTL, CACHE_TTI, Some(MAX_CAPACITY)));
|
||||
|
||||
/// CGraph Cache
|
||||
pub static CGRAPH_CACHE: Lazy<Cache> =
|
||||
Lazy::new(|| Cache::new(CACHE_TTL, CACHE_TTI, Some(MAX_CAPACITY)));
|
||||
|
||||
/// Trait which defines the behaviour of types that's gonna be stored in Cache
|
||||
pub trait Cacheable: Any + Send + Sync + DynClone {
|
||||
fn as_any(&self) -> &dyn Any;
|
||||
@ -48,6 +62,8 @@ pub trait Cacheable: Any + Send + Sync + DynClone {
|
||||
pub enum CacheKind<'a> {
|
||||
Config(Cow<'a, str>),
|
||||
Accounts(Cow<'a, str>),
|
||||
Routing(Cow<'a, str>),
|
||||
CGraph(Cow<'a, str>),
|
||||
All(Cow<'a, str>),
|
||||
}
|
||||
|
||||
@ -56,6 +72,8 @@ impl<'a> From<CacheKind<'a>> for RedisValue {
|
||||
let value = match kind {
|
||||
CacheKind::Config(s) => format!("{CONFIG_CACHE_PREFIX},{s}"),
|
||||
CacheKind::Accounts(s) => format!("{ACCOUNTS_CACHE_PREFIX},{s}"),
|
||||
CacheKind::Routing(s) => format!("{ROUTING_CACHE_PREFIX},{s}"),
|
||||
CacheKind::CGraph(s) => format!("{CGRAPH_CACHE_PREFIX},{s}"),
|
||||
CacheKind::All(s) => format!("{ALL_CACHE_PREFIX},{s}"),
|
||||
};
|
||||
Self::from_string(value)
|
||||
@ -73,6 +91,8 @@ impl<'a> TryFrom<RedisValue> for CacheKind<'a> {
|
||||
match split.0 {
|
||||
ACCOUNTS_CACHE_PREFIX => Ok(Self::Accounts(Cow::Owned(split.1.to_string()))),
|
||||
CONFIG_CACHE_PREFIX => Ok(Self::Config(Cow::Owned(split.1.to_string()))),
|
||||
ROUTING_CACHE_PREFIX => Ok(Self::Routing(Cow::Owned(split.1.to_string()))),
|
||||
CGRAPH_CACHE_PREFIX => Ok(Self::CGraph(Cow::Owned(split.1.to_string()))),
|
||||
ALL_CACHE_PREFIX => Ok(Self::All(Cow::Owned(split.1.to_string()))),
|
||||
_ => Err(validation_err.into()),
|
||||
}
|
||||
@ -123,6 +143,11 @@ impl Cache {
|
||||
(*val).as_any().downcast_ref::<T>().cloned()
|
||||
}
|
||||
|
||||
/// Check if a key exists in cache
|
||||
pub async fn exists(&self, key: &str) -> bool {
|
||||
self.inner.contains_key(key)
|
||||
}
|
||||
|
||||
pub async fn remove(&self, key: &str) {
|
||||
self.inner.invalidate(key).await;
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ use error_stack::ResultExt;
|
||||
use redis_interface::{errors as redis_errors, PubsubInterface, RedisValue};
|
||||
use router_env::logger;
|
||||
|
||||
use crate::redis::cache::{CacheKind, ACCOUNTS_CACHE, CONFIG_CACHE};
|
||||
use crate::redis::cache::{CacheKind, ACCOUNTS_CACHE, CGRAPH_CACHE, CONFIG_CACHE, ROUTING_CACHE};
|
||||
|
||||
#[async_trait::async_trait]
|
||||
pub trait PubSubInterface {
|
||||
@ -67,9 +67,19 @@ impl PubSubInterface for redis_interface::RedisConnectionPool {
|
||||
ACCOUNTS_CACHE.remove(key.as_ref()).await;
|
||||
key
|
||||
}
|
||||
CacheKind::CGraph(key) => {
|
||||
CGRAPH_CACHE.remove(key.as_ref()).await;
|
||||
key
|
||||
}
|
||||
CacheKind::Routing(key) => {
|
||||
ROUTING_CACHE.remove(key.as_ref()).await;
|
||||
key
|
||||
}
|
||||
CacheKind::All(key) => {
|
||||
CONFIG_CACHE.remove(key.as_ref()).await;
|
||||
ACCOUNTS_CACHE.remove(key.as_ref()).await;
|
||||
CGRAPH_CACHE.remove(key.as_ref()).await;
|
||||
ROUTING_CACHE.remove(key.as_ref()).await;
|
||||
key
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user