Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Add redis caching to getting user notifications and projects [MOD-540] (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
OmegaJak authored Oct 12, 2023
1 parent a951527 commit d9c6c29
Show file tree
Hide file tree
Showing 34 changed files with 848 additions and 379 deletions.
112 changes: 74 additions & 38 deletions sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -707,18 +707,6 @@
},
"query": "\n UPDATE mods\n SET webhook_sent = TRUE\n WHERE id = $1\n "
},
"127691940ca7e542e246dd2a1c9cb391041b30ddf0547d73b49c1dd9dc59d2ae": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int8Array"
]
}
},
"query": "\n UPDATE notifications\n SET read = TRUE\n WHERE id = ANY($1)\n "
},
"15fac93c76e72348b50f526e1acb183521d94be335ad8b9dfeb0398d4a8a2fc4": {
"describe": {
"columns": [],
Expand Down Expand Up @@ -2353,18 +2341,6 @@
},
"query": "\n SELECT id FROM threads\n WHERE report_id = $1\n "
},
"599df07263a2705e57fc70a7c4f5dc606e1730c281e3b573d2f2a2030bed04e0": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int8Array"
]
}
},
"query": "\n DELETE FROM notifications\n WHERE id = ANY($1)\n "
},
"59e95e832615c375753bfc9a56b07c02d916399adfa52fb11a79b8f7b56ecf8b": {
"describe": {
"columns": [
Expand Down Expand Up @@ -3720,6 +3696,20 @@
},
"query": "\n UPDATE pats\n SET expires = $1\n WHERE id = $2\n "
},
"8a9bf48b3d4aa665136568a9bf9ddb8e5d81ed27ce587e26672dfb45a44c7b9c": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int8Array",
"Int8Array",
"JsonbArray"
]
}
},
"query": "\n INSERT INTO notifications (\n id, user_id, body\n )\n SELECT * FROM UNNEST($1::bigint[], $2::bigint[], $3::jsonb[])\n "
},
"8abb317c85f48c7dd9ccf4a7b8fbc0b58ac73f7ae87ff2dfe67009a51089f784": {
"describe": {
"columns": [],
Expand Down Expand Up @@ -3861,6 +3851,26 @@
},
"query": "\n UPDATE threads\n SET show_in_mod_inbox = FALSE\n WHERE id = $1\n "
},
"8f74918aa923e516b6b2967b7d1afbd02c8bde5466d22ad60ad735f8358cbf04": {
"describe": {
"columns": [
{
"name": "user_id",
"ordinal": 0,
"type_info": "Int8"
}
],
"nullable": [
false
],
"parameters": {
"Left": [
"Int8"
]
}
},
"query": "\n DELETE FROM team_members\n WHERE team_id = $1\n RETURNING user_id\n "
},
"912250d37f13a98a21165c72bfc1eaa8a85b9952dd6750c117dca7fbb1bb8962": {
"describe": {
"columns": [],
Expand Down Expand Up @@ -5426,20 +5436,6 @@
},
"query": "\n DELETE FROM threads_messages\n WHERE thread_id = $1\n "
},
"d2c046d4bedeb7181ece4e94d7de90c97bd3dd1b0c16070704028923a0c2834a": {
"describe": {
"columns": [],
"nullable": [],
"parameters": {
"Left": [
"Int8",
"Int8",
"Jsonb"
]
}
},
"query": "\n INSERT INTO notifications (\n id, user_id, body\n )\n VALUES (\n $1, $2, $3\n )\n "
},
"d2e826d4fa4e3e730cc84c97964c0c5fdd25cd49ddff8c593bd9b8a3b4d5ff1e": {
"describe": {
"columns": [],
Expand Down Expand Up @@ -6291,6 +6287,26 @@
},
"query": "SELECT id FROM users WHERE discord_id = $1"
},
"ee375e658423156a758cc372400961f627fa5a620a3f61e37ec09fee1d7bb4e3": {
"describe": {
"columns": [
{
"name": "user_id",
"ordinal": 0,
"type_info": "Int8"
}
],
"nullable": [
false
],
"parameters": {
"Left": [
"Int8Array"
]
}
},
"query": "\n DELETE FROM notifications\n WHERE id = ANY($1)\n RETURNING user_id\n "
},
"eec6d4028d790e57a4d97fc5a200a9ae2b3d2cb60ee83c51fb05180b821558f5": {
"describe": {
"columns": [],
Expand Down Expand Up @@ -6470,6 +6486,26 @@
},
"query": "\n UPDATE users\n SET bio = $1\n WHERE (id = $2)\n "
},
"f775506213dbf4bf0ee05fd53c693412e3baae64b6dc0aead8082059f16755bc": {
"describe": {
"columns": [
{
"name": "user_id",
"ordinal": 0,
"type_info": "Int8"
}
],
"nullable": [
false
],
"parameters": {
"Left": [
"Int8Array"
]
}
},
"query": "\n UPDATE notifications\n SET read = TRUE\n WHERE id = ANY($1)\n RETURNING user_id\n "
},
"f793e96499ff35f8dc2e420484c2a0cdb54f25ffa27caa081691779ab896a709": {
"describe": {
"columns": [],
Expand Down
98 changes: 28 additions & 70 deletions src/database/models/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,9 @@ impl Category {
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
let res = redis
.get::<String, _>(TAGS_NAMESPACE, "category")
.await?
.and_then(|x| serde_json::from_str::<Vec<Category>>(&x).ok());
let res: Option<Vec<Category>> = redis
.get_deserialized_from_json(TAGS_NAMESPACE, "category")
.await?;

if let Some(res) = res {
return Ok(res);
Expand Down Expand Up @@ -133,12 +132,7 @@ impl Category {
.await?;

redis
.set(
TAGS_NAMESPACE,
"category",
serde_json::to_string(&result)?,
None,
)
.set_serialized_to_json(TAGS_NAMESPACE, "category", &result, None)
.await?;

Ok(result)
Expand Down Expand Up @@ -167,10 +161,9 @@ impl Loader {
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
let res = redis
.get::<String, _>(TAGS_NAMESPACE, "loader")
.await?
.and_then(|x| serde_json::from_str::<Vec<Loader>>(&x).ok());
let res: Option<Vec<Loader>> = redis
.get_deserialized_from_json(TAGS_NAMESPACE, "loader")
.await?;

if let Some(res) = res {
return Ok(res);
Expand Down Expand Up @@ -204,12 +197,7 @@ impl Loader {
.await?;

redis
.set(
TAGS_NAMESPACE,
"loader",
serde_json::to_string(&result)?,
None,
)
.set_serialized_to_json(TAGS_NAMESPACE, "loader", &result, None)
.await?;

Ok(result)
Expand Down Expand Up @@ -252,10 +240,9 @@ impl GameVersion {
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
let res = redis
.get::<String, _>(TAGS_NAMESPACE, "game_version")
.await?
.and_then(|x| serde_json::from_str::<Vec<GameVersion>>(&x).ok());
let res: Option<Vec<GameVersion>> = redis
.get_deserialized_from_json(TAGS_NAMESPACE, "game_version")
.await?;

if let Some(res) = res {
return Ok(res);
Expand All @@ -279,12 +266,7 @@ impl GameVersion {
.await?;

redis
.set(
TAGS_NAMESPACE,
"game_version",
serde_json::to_string(&result)?,
None,
)
.set_serialized_to_json(TAGS_NAMESPACE, "game_version", &result, None)
.await?;
Ok(result)
}
Expand Down Expand Up @@ -400,10 +382,9 @@ impl DonationPlatform {
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
let res = redis
.get::<String, _>(TAGS_NAMESPACE, "donation_platform")
.await?
.and_then(|x| serde_json::from_str::<Vec<DonationPlatform>>(&x).ok());
let res: Option<Vec<DonationPlatform>> = redis
.get_deserialized_from_json(TAGS_NAMESPACE, "donation_platform")
.await?;

if let Some(res) = res {
return Ok(res);
Expand All @@ -426,12 +407,7 @@ impl DonationPlatform {
.await?;

redis
.set(
TAGS_NAMESPACE,
"donation_platform",
serde_json::to_string(&result)?,
None,
)
.set_serialized_to_json(TAGS_NAMESPACE, "donation_platform", &result, None)
.await?;

Ok(result)
Expand Down Expand Up @@ -460,10 +436,9 @@ impl ReportType {
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
let res = redis
.get::<String, _>(TAGS_NAMESPACE, "report_type")
.await?
.and_then(|x| serde_json::from_str::<Vec<String>>(&x).ok());
let res: Option<Vec<String>> = redis
.get_deserialized_from_json(TAGS_NAMESPACE, "report_type")
.await?;

if let Some(res) = res {
return Ok(res);
Expand All @@ -480,12 +455,7 @@ impl ReportType {
.await?;

redis
.set(
TAGS_NAMESPACE,
"report_type",
serde_json::to_string(&result)?,
None,
)
.set_serialized_to_json(TAGS_NAMESPACE, "report_type", &result, None)
.await?;

Ok(result)
Expand Down Expand Up @@ -514,10 +484,9 @@ impl ProjectType {
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
let res = redis
.get::<String, _>(TAGS_NAMESPACE, "project_type")
.await?
.and_then(|x| serde_json::from_str::<Vec<String>>(&x).ok());
let res: Option<Vec<String>> = redis
.get_deserialized_from_json(TAGS_NAMESPACE, "project_type")
.await?;

if let Some(res) = res {
return Ok(res);
Expand All @@ -534,12 +503,7 @@ impl ProjectType {
.await?;

redis
.set(
TAGS_NAMESPACE,
"project_type",
serde_json::to_string(&result)?,
None,
)
.set_serialized_to_json(TAGS_NAMESPACE, "project_type", &result, None)
.await?;

Ok(result)
Expand Down Expand Up @@ -568,10 +532,9 @@ impl SideType {
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
let res = redis
.get::<String, _>(TAGS_NAMESPACE, "side_type")
.await?
.and_then(|x| serde_json::from_str::<Vec<String>>(&x).ok());
let res: Option<Vec<String>> = redis
.get_deserialized_from_json(TAGS_NAMESPACE, "side_type")
.await?;

if let Some(res) = res {
return Ok(res);
Expand All @@ -588,12 +551,7 @@ impl SideType {
.await?;

redis
.set(
TAGS_NAMESPACE,
"side_type",
serde_json::to_string(&result)?,
None,
)
.set_serialized_to_json(TAGS_NAMESPACE, "side_type", &result, None)
.await?;

Ok(result)
Expand Down
4 changes: 2 additions & 2 deletions src/database/models/collection_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,10 @@ impl Collection {

for collection in db_collections {
redis
.set(
.set_serialized_to_json(
COLLECTIONS_NAMESPACE,
collection.id.0,
serde_json::to_string(&collection)?,
&collection,
None,
)
.await?;
Expand Down
10 changes: 2 additions & 8 deletions src/database/models/flow_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,13 @@ impl Flow {
.collect::<String>();

redis
.set(
FLOWS_NAMESPACE,
&flow,
serde_json::to_string(&self)?,
Some(expires.num_seconds()),
)
.set_serialized_to_json(FLOWS_NAMESPACE, &flow, &self, Some(expires.num_seconds()))
.await?;
Ok(flow)
}

pub async fn get(id: &str, redis: &RedisPool) -> Result<Option<Flow>, DatabaseError> {
let res = redis.get::<String, _>(FLOWS_NAMESPACE, id).await?;
Ok(res.and_then(|x| serde_json::from_str(&x).ok()))
redis.get_deserialized_from_json(FLOWS_NAMESPACE, id).await
}

pub async fn remove(id: &str, redis: &RedisPool) -> Result<Option<()>, DatabaseError> {
Expand Down
Loading

0 comments on commit d9c6c29

Please sign in to comment.