feat: add accounts in-memory cache (#1086)

This commit is contained in:
Kartikeya Hegde
2023-05-15 18:03:49 +05:30
committed by GitHub
parent f31926b833
commit da4d721424
8 changed files with 150 additions and 31 deletions

View File

@ -6,13 +6,13 @@ pub mod logger;
use std::sync::{atomic, Arc};
use error_stack::{IntoReport, ResultExt};
use redis_interface::{errors as redis_errors, PubsubInterface};
use redis_interface::{errors as redis_errors, PubsubInterface, RedisValue};
use tokio::sync::oneshot;
pub use self::{api::*, encryption::*};
use crate::{
async_spawn,
cache::CONFIG_CACHE,
cache::{CacheKind, ACCOUNTS_CACHE, CONFIG_CACHE},
configs::settings,
connection::{diesel_make_pg_pool, PgPool},
consts,
@ -26,10 +26,10 @@ pub trait PubSubInterface {
channel: &str,
) -> errors::CustomResult<usize, redis_errors::RedisError>;
async fn publish(
async fn publish<'a>(
&self,
channel: &str,
key: &str,
key: CacheKind<'a>,
) -> errors::CustomResult<usize, redis_errors::RedisError>;
async fn on_message(&self) -> errors::CustomResult<(), redis_errors::RedisError>;
@ -50,13 +50,13 @@ impl PubSubInterface for redis_interface::RedisConnectionPool {
}
#[inline]
async fn publish(
async fn publish<'a>(
&self,
channel: &str,
key: &str,
key: CacheKind<'a>,
) -> errors::CustomResult<usize, redis_errors::RedisError> {
self.publisher
.publish(channel, key)
.publish(channel, RedisValue::from(key).into_inner())
.await
.into_report()
.change_context(redis_errors::RedisError::SubscribeError)
@ -66,13 +66,19 @@ impl PubSubInterface for redis_interface::RedisConnectionPool {
async fn on_message(&self) -> errors::CustomResult<(), redis_errors::RedisError> {
let mut rx = self.subscriber.on_message();
while let Ok(message) = rx.recv().await {
let key = message
.value
.as_string()
.ok_or::<redis_errors::RedisError>(redis_errors::RedisError::DeleteFailed)?;
self.delete_key(&key).await?;
CONFIG_CACHE.invalidate(&key).await;
let key: CacheKind<'_> = RedisValue::new(message.value)
.try_into()
.change_context(redis_errors::RedisError::OnMessageError)?;
match key {
CacheKind::Config(key) => {
self.delete_key(key.as_ref()).await?;
CONFIG_CACHE.invalidate(key.as_ref()).await;
}
CacheKind::Accounts(key) => {
self.delete_key(key.as_ref()).await?;
ACCOUNTS_CACHE.invalidate(key.as_ref()).await;
}
}
}
Ok(())
}