-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(caching): implement hash_table and merchant table caching (#55)
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
358cdb8
commit f0d4cc4
Showing
12 changed files
with
818 additions
and
316 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use std::sync::Arc; | ||
|
||
#[derive(Clone)] | ||
pub struct Caching<T, U> | ||
where | ||
T: super::Cacheable<U>, | ||
{ | ||
inner: T, | ||
cache: moka::future::Cache<T::Key, Arc<T::Value>>, | ||
} | ||
|
||
// impl<U, T: super::Cacheable<U>> super::Cacheable<U> for Caching<T, U> { | ||
// type Key = T::Key; | ||
// type Value = T::Value; | ||
// } | ||
|
||
impl<U1, U2, T> super::Cacheable<U2> for Caching<T, U1> | ||
where | ||
T: super::Cacheable<U2> + super::Cacheable<U1>, | ||
{ | ||
type Key = <T as super::Cacheable<U2>>::Key; | ||
|
||
type Value = <T as super::Cacheable<U2>>::Value; | ||
} | ||
|
||
impl<T: super::Cacheable<U>, U> std::ops::Deref for Caching<T, U> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.inner | ||
} | ||
} | ||
|
||
impl<T, U> Caching<T, U> | ||
where | ||
T: super::Cacheable<U>, | ||
{ | ||
#[inline(always)] | ||
pub async fn lookup(&self, key: T::Key) -> Option<T::Value> { | ||
self.cache.get(&key).await.map(|value| { | ||
let data = value.as_ref(); | ||
data.clone() | ||
}) | ||
} | ||
|
||
#[inline(always)] | ||
pub async fn cache_data(&self, key: T::Key, value: T::Value) { | ||
self.cache.insert(key, value.into()).await; | ||
} | ||
} | ||
|
||
pub fn implement_cache<'a, T, U>( | ||
name: &'a str, | ||
config: &'a crate::config::Cache, | ||
) -> impl Fn(T) -> Caching<T, U> + 'a | ||
where | ||
T: super::Cacheable<U>, | ||
{ | ||
// Caching { inner, cache } | ||
move |inner| { | ||
let cache = moka::future::CacheBuilder::new(config.max_capacity).name(name); | ||
let cache = match config.tti { | ||
Some(value) => cache.time_to_idle(std::time::Duration::from_secs(value)), | ||
None => cache, | ||
}; | ||
|
||
Caching { | ||
inner, | ||
cache: cache.build(), | ||
} | ||
} | ||
} | ||
|
||
pub mod hash_table; | ||
pub mod merchant; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use crate::{ | ||
error::ContainerError, | ||
storage::{self, types}, | ||
}; | ||
|
||
#[async_trait::async_trait] | ||
impl<T> storage::HashInterface for super::Caching<T, types::HashTable> | ||
where | ||
T: storage::HashInterface | ||
+ storage::Cacheable<types::HashTable, Key = Vec<u8>, Value = types::HashTable> | ||
+ Sync | ||
+ Send, | ||
{ | ||
type Error = T::Error; | ||
|
||
async fn find_by_data_hash( | ||
&self, | ||
data_hash: &[u8], | ||
) -> Result<Option<types::HashTable>, ContainerError<Self::Error>> { | ||
match self.lookup(data_hash.to_vec()).await { | ||
value @ Some(_) => Ok(value), | ||
None => Ok(match self.inner.find_by_data_hash(data_hash).await? { | ||
None => None, | ||
Some(value) => { | ||
self.cache_data(data_hash.to_vec(), value.clone()).await; | ||
Some(value) | ||
} | ||
}), | ||
} | ||
} | ||
|
||
async fn insert_hash( | ||
&self, | ||
data_hash: Vec<u8>, | ||
) -> Result<types::HashTable, ContainerError<Self::Error>> { | ||
let output = self.inner.insert_hash(data_hash.clone()).await?; | ||
self.cache_data(data_hash, output.clone()).await; | ||
Ok(output) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use crate::{ | ||
error::ContainerError, | ||
storage::{self, types}, | ||
}; | ||
|
||
#[async_trait::async_trait] | ||
impl<T> storage::MerchantInterface for super::Caching<T, types::Merchant> | ||
where | ||
T: storage::MerchantInterface | ||
+ storage::Cacheable<types::Merchant, Key = (String, String), Value = types::Merchant> | ||
+ Sync | ||
+ Send, | ||
{ | ||
type Algorithm = T::Algorithm; | ||
type Error = T::Error; | ||
|
||
async fn find_by_merchant_id( | ||
&self, | ||
merchant_id: &str, | ||
tenant_id: &str, | ||
key: &Self::Algorithm, | ||
) -> Result<types::Merchant, ContainerError<Self::Error>> { | ||
let cached_data = self | ||
.lookup((tenant_id.to_string(), merchant_id.to_string())) | ||
.await; | ||
match cached_data { | ||
Some(value) => Ok(value), | ||
None => { | ||
let output = self | ||
.inner | ||
.find_by_merchant_id(merchant_id, tenant_id, key) | ||
.await?; | ||
self.cache_data( | ||
(output.tenant_id.to_string(), output.merchant_id.to_string()), | ||
output.clone(), | ||
) | ||
.await; | ||
Ok(output) | ||
} | ||
} | ||
} | ||
|
||
async fn find_or_create_by_merchant_id( | ||
&self, | ||
merchant_id: &str, | ||
tenant_id: &str, | ||
key: &Self::Algorithm, | ||
) -> Result<types::Merchant, ContainerError<Self::Error>> { | ||
self.inner | ||
.find_or_create_by_merchant_id(merchant_id, tenant_id, key) | ||
.await | ||
} | ||
|
||
async fn insert_merchant( | ||
&self, | ||
new: types::MerchantNew<'_>, | ||
key: &Self::Algorithm, | ||
) -> Result<types::Merchant, ContainerError<Self::Error>> { | ||
let merchant_id = new.merchant_id.to_string(); | ||
let tenant_id = new.tenant_id.to_string(); | ||
let output = self.inner.insert_merchant(new, key).await?; | ||
self.cache_data((tenant_id, merchant_id), output.clone()) | ||
.await; | ||
Ok(output) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters