Add redis caching to getting user notifications and projects [MOD-540] (#723)

* Add redis caching to getting a user's project ids

* Run `cargo sqlx prepare` to update the sqlx-data.json

* Add redis caching for getting user notifications

* Fix new clippy warnings

* Remove log that shouldn't have been committed

* Batch insert of notifications (untested)

* sqlx prepare...

* Fix merge conflict things and use new redis struct

* Fix bug with calling delete_many without any elements (caught by tests)

* cargo sqlx prepare

* Add tests around cache invalidation (and fix bug they caught!)

* Some test reorg based on code review suggestions
This commit is contained in:
Jackson Kruger
2023-10-12 17:52:24 -05:00
committed by GitHub
parent d66270eef0
commit abf4cd71ba
34 changed files with 848 additions and 379 deletions

View File

@@ -59,9 +59,24 @@ impl RedisPool {
Ok(())
}
pub async fn get<R, T1>(&self, namespace: &str, id: T1) -> Result<Option<R>, DatabaseError>
pub async fn set_serialized_to_json<Id, D>(
&self,
namespace: &str,
id: Id,
data: D,
expiry: Option<i64>,
) -> Result<(), DatabaseError>
where
T1: Display,
Id: Display,
D: serde::Serialize,
{
self.set(namespace, id, serde_json::to_string(&data)?, expiry)
.await
}
pub async fn get<R, Id>(&self, namespace: &str, id: Id) -> Result<Option<R>, DatabaseError>
where
Id: Display,
R: FromRedisValue,
{
let mut redis_connection = self.pool.get().await?;
@@ -73,6 +88,21 @@ impl RedisPool {
Ok(res)
}
pub async fn get_deserialized_from_json<R, Id>(
&self,
namespace: &str,
id: Id,
) -> Result<Option<R>, DatabaseError>
where
Id: Display,
R: for<'a> serde::Deserialize<'a>,
{
Ok(self
.get::<String, Id>(namespace, id)
.await?
.and_then(|x| serde_json::from_str(&x).ok()))
}
pub async fn multi_get<R, T1>(
&self,
namespace: &str,
@@ -111,17 +141,20 @@ impl RedisPool {
pub async fn delete_many(
&self,
iter: impl IntoIterator<Item = (&str, Option<String>)>,
) -> Result<(), DatabaseError>
where {
let mut redis_connection = self.pool.get().await?;
) -> Result<(), DatabaseError> {
let mut cmd = cmd("DEL");
let mut any = false;
for (namespace, id) in iter {
if let Some(id) = id {
cmd.arg(format!("{}_{}:{}", self.meta_namespace, namespace, id));
any = true;
}
}
cmd.query_async::<_, ()>(&mut redis_connection).await?;
if any {
let mut redis_connection = self.pool.get().await?;
cmd.query_async::<_, ()>(&mut redis_connection).await?;
}
Ok(())
}