refactor(cache): remove deref impl on Cache type (#4671)

This commit is contained in:
Chethan Rao
2024-05-20 19:14:41 +05:30
committed by GitHub
parent 842728ef93
commit 36409bdc91
4 changed files with 17 additions and 18 deletions

View File

@ -94,13 +94,6 @@ pub struct Cache {
inner: MokaCache<String, Arc<dyn Cacheable>>,
}
impl std::ops::Deref for Cache {
type Target = MokaCache<String, Arc<dyn Cacheable>>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl Cache {
/// With given `time_to_live` and `time_to_idle` creates a moka cache.
///
@ -122,16 +115,16 @@ impl Cache {
}
pub async fn push<T: Cacheable>(&self, key: String, val: T) {
self.insert(key, Arc::new(val)).await;
self.inner.insert(key, Arc::new(val)).await;
}
pub async fn get_val<T: Clone + Cacheable>(&self, key: &str) -> Option<T> {
let val = self.get(key).await?;
let val = self.inner.get(key).await?;
(*val).as_any().downcast_ref::<T>().cloned()
}
pub async fn remove(&self, key: &str) {
self.invalidate(key).await;
self.inner.invalidate(key).await;
}
}
@ -208,7 +201,7 @@ where
Fut: futures::Future<Output = CustomResult<T, StorageError>> + Send,
{
let data = fun().await?;
in_memory.async_map(|cache| cache.invalidate(key)).await;
in_memory.async_map(|cache| cache.remove(key)).await;
let redis_conn = store
.get_redis_conn()

View File

@ -60,16 +60,16 @@ impl PubSubInterface for redis_interface::RedisConnectionPool {
let key = match key {
CacheKind::Config(key) => {
CONFIG_CACHE.invalidate(key.as_ref()).await;
CONFIG_CACHE.remove(key.as_ref()).await;
key
}
CacheKind::Accounts(key) => {
ACCOUNTS_CACHE.invalidate(key.as_ref()).await;
ACCOUNTS_CACHE.remove(key.as_ref()).await;
key
}
CacheKind::All(key) => {
CONFIG_CACHE.invalidate(key.as_ref()).await;
ACCOUNTS_CACHE.invalidate(key.as_ref()).await;
CONFIG_CACHE.remove(key.as_ref()).await;
ACCOUNTS_CACHE.remove(key.as_ref()).await;
key
}
};