From 4f724a371c8ebebd80df9709a227b2eb9f6a95e5 Mon Sep 17 00:00:00 2001 From: Diptesh Choudhuri Date: Sun, 15 Dec 2024 11:10:48 +0530 Subject: [PATCH] refactor(backend): more generic structs for cache key --- crates/models/common/src/lib.rs | 34 +++++++++-------- crates/services/miscellaneous/src/lib.rs | 24 ++++++------ crates/utils/dependent/src/lib.rs | 47 ++++++++++++++---------- 3 files changed, 60 insertions(+), 45 deletions(-) diff --git a/crates/models/common/src/lib.rs b/crates/models/common/src/lib.rs index 9f6af28d36..db06d89960 100644 --- a/crates/models/common/src/lib.rs +++ b/crates/models/common/src/lib.rs @@ -300,6 +300,23 @@ pub struct UserLevelCacheKey { pub user_id: String, } +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct MetadataRecentlyConsumedCacheInput { + pub entity_id: String, + pub entity_lot: EntityLot, +} + +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct ProgressUpdateCacheInput { + pub metadata_id: String, + pub show_season_number: Option, + pub manga_volume_number: Option, + pub show_episode_number: Option, + pub anime_episode_number: Option, + pub podcast_episode_number: Option, + pub manga_chapter_number: Option, +} + #[skip_serializing_none] #[derive(Clone, Debug, PartialEq, FromJsonQueryResult, Eq, Serialize, Deserialize)] pub enum ApplicationCacheKey { @@ -312,19 +329,6 @@ pub enum ApplicationCacheKey { UserAnalytics(UserLevelCacheKey), MetadataSearch(UserLevelCacheKey), MetadataGroupSearch(UserLevelCacheKey), - MetadataRecentlyConsumed { - user_id: String, - entity_id: String, - entity_lot: EntityLot, - }, - ProgressUpdateCache { - user_id: String, - metadata_id: String, - show_season_number: Option, - show_episode_number: Option, - podcast_episode_number: Option, - anime_episode_number: Option, - manga_chapter_number: Option, - manga_volume_number: Option, - }, + ProgressUpdateCache(UserLevelCacheKey), + MetadataRecentlyConsumed(UserLevelCacheKey), } diff --git a/crates/services/miscellaneous/src/lib.rs b/crates/services/miscellaneous/src/lib.rs index ae6a29a864..94aec50d72 100644 --- a/crates/services/miscellaneous/src/lib.rs +++ b/crates/services/miscellaneous/src/lib.rs @@ -14,8 +14,8 @@ use chrono::{Days, Duration, NaiveDate, TimeZone, Utc}; use common_models::{ ApplicationCacheKey, BackendError, BackgroundJob, ChangeCollectionToEntityInput, DefaultCollection, IdAndNamedObject, MediaStateChanged, MetadataGroupSearchInput, - MetadataSearchInput, PeopleSearchInput, SearchDetails, SearchInput, StoredUrl, StringIdObject, - UserLevelCacheKey, + MetadataSearchInput, PeopleSearchInput, ProgressUpdateCacheInput, SearchDetails, SearchInput, + StoredUrl, StringIdObject, UserLevelCacheKey, }; use common_utils::{ convert_naive_to_utc, get_first_and_last_day_of_month, ryot_log, IsFeatureEnabled, @@ -1927,16 +1927,18 @@ ORDER BY RANDOM() LIMIT 10; let aen = si.anime_extra_information.as_ref().and_then(|d| d.episode); let mcn = si.manga_extra_information.as_ref().and_then(|d| d.chapter); let mvn = si.manga_extra_information.as_ref().and_then(|d| d.volume); - let cache = ApplicationCacheKey::ProgressUpdateCache { - show_season_number: ssn, - manga_volume_number: mvn, - show_episode_number: sen, - anime_episode_number: aen, - manga_chapter_number: mcn, - podcast_episode_number: pen, + let cache = ApplicationCacheKey::ProgressUpdateCache(UserLevelCacheKey { user_id: user_id.to_owned(), - metadata_id: si.metadata_id.clone(), - }; + input: ProgressUpdateCacheInput { + show_season_number: ssn, + manga_volume_number: mvn, + show_episode_number: sen, + anime_episode_number: aen, + manga_chapter_number: mcn, + podcast_episode_number: pen, + metadata_id: si.metadata_id.clone(), + }, + }); self.0.cache_service.expire_key(cache).await?; let seen_id = si.id.clone(); let metadata_id = si.metadata_id.clone(); diff --git a/crates/utils/dependent/src/lib.rs b/crates/utils/dependent/src/lib.rs index 00f7585a7c..02f89f8676 100644 --- a/crates/utils/dependent/src/lib.rs +++ b/crates/utils/dependent/src/lib.rs @@ -6,7 +6,8 @@ use background::{ApplicationJob, CoreApplicationJob}; use chrono::Utc; use common_models::{ ApplicationCacheKey, BackgroundJob, ChangeCollectionToEntityInput, DefaultCollection, - MediaStateChanged, StoredUrl, StringIdObject, + MediaStateChanged, MetadataRecentlyConsumedCacheInput, ProgressUpdateCacheInput, StoredUrl, + StringIdObject, UserLevelCacheKey, }; use common_utils::{ryot_log, EXERCISE_LOT_MAPPINGS, SHOW_SPECIAL_SEASON_NAMES}; use database_models::{ @@ -1261,11 +1262,13 @@ pub async fn mark_entity_as_recently_consumed( ) -> Result<()> { ss.cache_service .set_key( - ApplicationCacheKey::MetadataRecentlyConsumed { - entity_lot, + ApplicationCacheKey::MetadataRecentlyConsumed(UserLevelCacheKey { user_id: user_id.to_owned(), - entity_id: entity_id.to_owned(), - }, + input: MetadataRecentlyConsumedCacheInput { + entity_lot, + entity_id: entity_id.to_owned(), + }, + }), ApplicationCacheValue::Empty(EmptyCacheValue::default()), ) .await?; @@ -1280,11 +1283,15 @@ pub async fn get_entity_recently_consumed( ) -> Result { Ok(ss .cache_service - .get_value::(ApplicationCacheKey::MetadataRecentlyConsumed { - entity_lot, - user_id: user_id.to_owned(), - entity_id: entity_id.to_owned(), - }) + .get_value::(ApplicationCacheKey::MetadataRecentlyConsumed( + UserLevelCacheKey { + user_id: user_id.to_owned(), + input: MetadataRecentlyConsumedCacheInput { + entity_lot, + entity_id: entity_id.to_owned(), + }, + }, + )) .await? .is_some()) } @@ -1296,16 +1303,18 @@ pub async fn progress_update( input: ProgressUpdateInput, ss: &Arc, ) -> Result { - let cache = ApplicationCacheKey::ProgressUpdateCache { + let cache = ApplicationCacheKey::ProgressUpdateCache(UserLevelCacheKey { user_id: user_id.to_owned(), - metadata_id: input.metadata_id.clone(), - show_season_number: input.show_season_number, - show_episode_number: input.show_episode_number, - manga_volume_number: input.manga_volume_number, - manga_chapter_number: input.manga_chapter_number, - anime_episode_number: input.anime_episode_number, - podcast_episode_number: input.podcast_episode_number, - }; + input: ProgressUpdateCacheInput { + metadata_id: input.metadata_id.clone(), + show_season_number: input.show_season_number, + show_episode_number: input.show_episode_number, + manga_volume_number: input.manga_volume_number, + manga_chapter_number: input.manga_chapter_number, + anime_episode_number: input.anime_episode_number, + podcast_episode_number: input.podcast_episode_number, + }, + }); if respect_cache { let in_cache = ss .cache_service