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

@@ -1,13 +1,18 @@
use super::ids::*;
use crate::database::models::DatabaseError;
use crate::database::{models::DatabaseError, redis::RedisPool};
use crate::models::notifications::NotificationBody;
use chrono::{DateTime, Utc};
use serde::Deserialize;
use futures::TryStreamExt;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
const USER_NOTIFICATIONS_NAMESPACE: &str = "user_notifications";
pub struct NotificationBuilder {
pub body: NotificationBody,
}
#[derive(Serialize, Deserialize)]
pub struct Notification {
pub id: NotificationId,
pub user_id: UserId,
@@ -16,7 +21,7 @@ pub struct Notification {
pub created: DateTime<Utc>,
}
#[derive(Deserialize)]
#[derive(Serialize, Deserialize)]
pub struct NotificationAction {
pub id: NotificationActionId,
pub notification_id: NotificationId,
@@ -30,54 +35,68 @@ impl NotificationBuilder {
&self,
user: UserId,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
redis: &RedisPool,
) -> Result<(), DatabaseError> {
self.insert_many(vec![user], transaction).await
self.insert_many(vec![user], transaction, redis).await
}
pub async fn insert_many(
&self,
users: Vec<UserId>,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
redis: &RedisPool,
) -> Result<(), DatabaseError> {
let mut notifications = Vec::new();
for user in users {
let id = generate_notification_id(&mut *transaction).await?;
Notification {
notifications.push(Notification {
id,
user_id: user,
body: self.body.clone(),
read: false,
created: Utc::now(),
}
.insert(&mut *transaction)
.await?;
});
}
Notification::insert_many(&notifications, transaction, redis).await?;
Ok(())
}
}
impl Notification {
pub async fn insert(
&self,
pub async fn insert_many(
notifications: &[Notification],
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
redis: &RedisPool,
) -> Result<(), DatabaseError> {
let notification_ids = notifications.iter().map(|n| n.id.0).collect_vec();
let user_ids = notifications.iter().map(|n| n.user_id.0).collect_vec();
let bodies = notifications
.iter()
.map(|n| Ok(serde_json::value::to_value(n.body.clone())?))
.collect::<Result<Vec<_>, DatabaseError>>()?;
sqlx::query!(
"
INSERT INTO notifications (
id, user_id, body
)
VALUES (
$1, $2, $3
)
SELECT * FROM UNNEST($1::bigint[], $2::bigint[], $3::jsonb[])
",
self.id as NotificationId,
self.user_id as UserId,
serde_json::value::to_value(self.body.clone())?
&notification_ids[..],
&user_ids[..],
&bodies[..],
)
.execute(&mut *transaction)
.await?;
Notification::clear_user_notifications_cache(
notifications.iter().map(|n| &n.user_id),
redis,
)
.await?;
Ok(())
}
@@ -100,8 +119,6 @@ impl Notification {
where
E: sqlx::Executor<'a, Database = sqlx::Postgres> + Copy,
{
use futures::stream::TryStreamExt;
let notification_ids_parsed: Vec<i64> = notification_ids.iter().map(|x| x.0).collect();
sqlx::query!(
"
@@ -152,13 +169,20 @@ impl Notification {
pub async fn get_many_user<'a, E>(
user_id: UserId,
exec: E,
) -> Result<Vec<Notification>, sqlx::Error>
redis: &RedisPool,
) -> Result<Vec<Notification>, DatabaseError>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres> + Copy,
{
use futures::stream::TryStreamExt;
let cached_notifications: Option<Vec<Notification>> = redis
.get_deserialized_from_json(USER_NOTIFICATIONS_NAMESPACE, user_id.0)
.await?;
sqlx::query!(
if let Some(notifications) = cached_notifications {
return Ok(notifications);
}
let db_notifications = sqlx::query!(
"
SELECT n.id, n.user_id, n.title, n.text, n.link, n.created, n.read, n.type notification_type, n.body,
JSONB_AGG(DISTINCT jsonb_build_object('id', na.id, 'notification_id', na.notification_id, 'title', na.title, 'action_route_method', na.action_route_method, 'action_route', na.action_route)) filter (where na.id is not null) actions
@@ -200,47 +224,67 @@ impl Notification {
}))
})
.try_collect::<Vec<Notification>>()
.await
.await?;
redis
.set_serialized_to_json(
USER_NOTIFICATIONS_NAMESPACE,
user_id.0,
&db_notifications,
None,
)
.await?;
Ok(db_notifications)
}
pub async fn read(
id: NotificationId,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<Option<()>, sqlx::error::Error> {
Self::read_many(&[id], transaction).await
redis: &RedisPool,
) -> Result<Option<()>, DatabaseError> {
Self::read_many(&[id], transaction, redis).await
}
pub async fn read_many(
notification_ids: &[NotificationId],
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<Option<()>, sqlx::error::Error> {
redis: &RedisPool,
) -> Result<Option<()>, DatabaseError> {
let notification_ids_parsed: Vec<i64> = notification_ids.iter().map(|x| x.0).collect();
sqlx::query!(
let affected_users = sqlx::query!(
"
UPDATE notifications
SET read = TRUE
WHERE id = ANY($1)
RETURNING user_id
",
&notification_ids_parsed
)
.execute(&mut *transaction)
.fetch_many(&mut *transaction)
.try_filter_map(|e| async { Ok(e.right().map(|x| UserId(x.user_id))) })
.try_collect::<Vec<_>>()
.await?;
Notification::clear_user_notifications_cache(affected_users.iter(), redis).await?;
Ok(Some(()))
}
pub async fn remove(
id: NotificationId,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<Option<()>, sqlx::error::Error> {
Self::remove_many(&[id], transaction).await
redis: &RedisPool,
) -> Result<Option<()>, DatabaseError> {
Self::remove_many(&[id], transaction, redis).await
}
pub async fn remove_many(
notification_ids: &[NotificationId],
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<Option<()>, sqlx::error::Error> {
redis: &RedisPool,
) -> Result<Option<()>, DatabaseError> {
let notification_ids_parsed: Vec<i64> = notification_ids.iter().map(|x| x.0).collect();
sqlx::query!(
@@ -253,16 +297,36 @@ impl Notification {
.execute(&mut *transaction)
.await?;
sqlx::query!(
let affected_users = sqlx::query!(
"
DELETE FROM notifications
WHERE id = ANY($1)
RETURNING user_id
",
&notification_ids_parsed
)
.execute(&mut *transaction)
.fetch_many(&mut *transaction)
.try_filter_map(|e| async { Ok(e.right().map(|x| UserId(x.user_id))) })
.try_collect::<Vec<_>>()
.await?;
Notification::clear_user_notifications_cache(affected_users.iter(), redis).await?;
Ok(Some(()))
}
pub async fn clear_user_notifications_cache(
user_ids: impl IntoIterator<Item = &UserId>,
redis: &RedisPool,
) -> Result<(), DatabaseError> {
redis
.delete_many(
user_ids
.into_iter()
.map(|id| (USER_NOTIFICATIONS_NAMESPACE, Some(id.0.to_string()))),
)
.await?;
Ok(())
}
}