From 02e25d0e3eed2d78e92021af37bc7394e3f333a0 Mon Sep 17 00:00:00 2001 From: Diptesh Choudhuri Date: Sun, 15 Dec 2024 11:03:10 +0530 Subject: [PATCH] refactor(backend): generic struct for cache key --- crates/models/common/src/lib.rs | 32 +++++++++--------------- crates/services/miscellaneous/src/lib.rs | 13 +++++----- crates/services/statistics/src/lib.rs | 18 ++++++------- 3 files changed, 26 insertions(+), 37 deletions(-) diff --git a/crates/models/common/src/lib.rs b/crates/models/common/src/lib.rs index 9b50760791..9f6af28d36 100644 --- a/crates/models/common/src/lib.rs +++ b/crates/models/common/src/lib.rs @@ -294,32 +294,24 @@ pub struct MetadataSearchInput { pub source: MediaSource, } +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct UserLevelCacheKey { + pub input: T, + pub user_id: String, +} + #[skip_serializing_none] #[derive(Clone, Debug, PartialEq, FromJsonQueryResult, Eq, Serialize, Deserialize)] pub enum ApplicationCacheKey { IgdbSettings, TmdbSettings, - ListennotesSettings, ServerKeyValidated, - UserAnalyticsParameters { - user_id: String, - }, - MetadataSearch { - user_id: String, - input: MetadataSearchInput, - }, - PeopleSearch { - user_id: String, - input: PeopleSearchInput, - }, - MetadataGroupSearch { - user_id: String, - input: MetadataGroupSearchInput, - }, - UserAnalytics { - user_id: String, - input: UserAnalyticsInput, - }, + ListennotesSettings, + UserAnalyticsParameters(UserLevelCacheKey<()>), + PeopleSearch(UserLevelCacheKey), + UserAnalytics(UserLevelCacheKey), + MetadataSearch(UserLevelCacheKey), + MetadataGroupSearch(UserLevelCacheKey), MetadataRecentlyConsumed { user_id: String, entity_id: String, diff --git a/crates/services/miscellaneous/src/lib.rs b/crates/services/miscellaneous/src/lib.rs index ee15b61579..ae6a29a864 100644 --- a/crates/services/miscellaneous/src/lib.rs +++ b/crates/services/miscellaneous/src/lib.rs @@ -15,6 +15,7 @@ use common_models::{ ApplicationCacheKey, BackendError, BackgroundJob, ChangeCollectionToEntityInput, DefaultCollection, IdAndNamedObject, MediaStateChanged, MetadataGroupSearchInput, MetadataSearchInput, PeopleSearchInput, SearchDetails, SearchInput, StoredUrl, StringIdObject, + UserLevelCacheKey, }; use common_utils::{ convert_naive_to_utc, get_first_and_last_day_of_month, ryot_log, IsFeatureEnabled, @@ -1660,10 +1661,10 @@ ORDER BY RANDOM() LIMIT 10; user_id: &String, input: MetadataSearchInput, ) -> Result { - let cache_key = ApplicationCacheKey::MetadataSearch { + let cache_key = ApplicationCacheKey::MetadataSearch(UserLevelCacheKey { input: input.clone(), user_id: user_id.to_owned(), - }; + }); if let Some(cached) = self.0.cache_service.get_value(cache_key.clone()).await? { return Ok(cached); } @@ -1750,10 +1751,10 @@ ORDER BY RANDOM() LIMIT 10; user_id: &String, input: PeopleSearchInput, ) -> Result { - let cache_key = ApplicationCacheKey::PeopleSearch { + let cache_key = ApplicationCacheKey::PeopleSearch(UserLevelCacheKey { input: input.clone(), user_id: user_id.clone(), - }; + }); if let Some(results) = self.0.cache_service.get_value(cache_key.clone()).await? { return Ok(results); } @@ -1792,10 +1793,10 @@ ORDER BY RANDOM() LIMIT 10; user_id: &String, input: MetadataGroupSearchInput, ) -> Result { - let cache_key = ApplicationCacheKey::MetadataGroupSearch { + let cache_key = ApplicationCacheKey::MetadataGroupSearch(UserLevelCacheKey { input: input.clone(), user_id: user_id.clone(), - }; + }); if let Some(results) = self.0.cache_service.get_value(cache_key.clone()).await? { return Ok(results); } diff --git a/crates/services/statistics/src/lib.rs b/crates/services/statistics/src/lib.rs index be76839af1..007588debf 100644 --- a/crates/services/statistics/src/lib.rs +++ b/crates/services/statistics/src/lib.rs @@ -3,7 +3,7 @@ use std::{cmp::Reverse, fmt::Write, sync::Arc}; use async_graphql::Result; use common_models::{ ApplicationCacheKey, ApplicationDateRange, DailyUserActivitiesResponseGroupedBy, - DailyUserActivityHourRecord, UserAnalyticsInput, + DailyUserActivityHourRecord, UserAnalyticsInput, UserLevelCacheKey, }; use database_models::{daily_user_activity, prelude::DailyUserActivity}; use database_utils::calculate_user_activities_and_summary; @@ -38,15 +38,11 @@ impl StatisticsService { &self, user_id: &String, ) -> Result { - let cache_key = ApplicationCacheKey::UserAnalyticsParameters { + let cache_key = ApplicationCacheKey::UserAnalyticsParameters(UserLevelCacheKey { + input: (), user_id: user_id.to_owned(), - }; - if let Some(cached) = self - .0 - .cache_service - .get_value::(cache_key.clone()) - .await? - { + }); + if let Some(cached) = self.0.cache_service.get_value(cache_key.clone()).await? { return Ok(cached); } let get_date = |ordering: Order| { @@ -274,10 +270,10 @@ impl StatisticsService { user_id: &String, input: UserAnalyticsInput, ) -> Result { - let cache_key = ApplicationCacheKey::UserAnalytics { + let cache_key = ApplicationCacheKey::UserAnalytics(UserLevelCacheKey { input: input.clone(), user_id: user_id.to_owned(), - }; + }); if let Some(cached) = self .0 .cache_service