Skip to content

Commit

Permalink
refactor(backend): more generic structs for cache key
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnisDa committed Dec 15, 2024
1 parent 02e25d0 commit 4f724a3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 45 deletions.
34 changes: 19 additions & 15 deletions crates/models/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,23 @@ pub struct UserLevelCacheKey<T> {
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<i32>,
pub manga_volume_number: Option<i32>,
pub show_episode_number: Option<i32>,
pub anime_episode_number: Option<i32>,
pub podcast_episode_number: Option<i32>,
pub manga_chapter_number: Option<Decimal>,
}

#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, FromJsonQueryResult, Eq, Serialize, Deserialize)]
pub enum ApplicationCacheKey {
Expand All @@ -312,19 +329,6 @@ pub enum ApplicationCacheKey {
UserAnalytics(UserLevelCacheKey<UserAnalyticsInput>),
MetadataSearch(UserLevelCacheKey<MetadataSearchInput>),
MetadataGroupSearch(UserLevelCacheKey<MetadataGroupSearchInput>),
MetadataRecentlyConsumed {
user_id: String,
entity_id: String,
entity_lot: EntityLot,
},
ProgressUpdateCache {
user_id: String,
metadata_id: String,
show_season_number: Option<i32>,
show_episode_number: Option<i32>,
podcast_episode_number: Option<i32>,
anime_episode_number: Option<i32>,
manga_chapter_number: Option<Decimal>,
manga_volume_number: Option<i32>,
},
ProgressUpdateCache(UserLevelCacheKey<ProgressUpdateCacheInput>),
MetadataRecentlyConsumed(UserLevelCacheKey<MetadataRecentlyConsumedCacheInput>),
}
24 changes: 13 additions & 11 deletions crates/services/miscellaneous/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
47 changes: 28 additions & 19 deletions crates/utils/dependent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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?;
Expand All @@ -1280,11 +1283,15 @@ pub async fn get_entity_recently_consumed(
) -> Result<bool> {
Ok(ss
.cache_service
.get_value::<EmptyCacheValue>(ApplicationCacheKey::MetadataRecentlyConsumed {
entity_lot,
user_id: user_id.to_owned(),
entity_id: entity_id.to_owned(),
})
.get_value::<EmptyCacheValue>(ApplicationCacheKey::MetadataRecentlyConsumed(
UserLevelCacheKey {
user_id: user_id.to_owned(),
input: MetadataRecentlyConsumedCacheInput {
entity_lot,
entity_id: entity_id.to_owned(),
},
},
))
.await?
.is_some())
}
Expand All @@ -1296,16 +1303,18 @@ pub async fn progress_update(
input: ProgressUpdateInput,
ss: &Arc<SupportingService>,
) -> Result<ProgressUpdateResultUnion> {
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
Expand Down

0 comments on commit 4f724a3

Please sign in to comment.