From 08abb539235dbd4b3a5deb40bfbd4727973d3aa3 Mon Sep 17 00:00:00 2001 From: Jamie Rees Date: Tue, 7 May 2019 13:34:39 +0100 Subject: [PATCH] Added caching all over the new API's this will help with the API limits that we will now easily reach due to the infinate scroll --- src/Ombi.Core/Engine/MovieSearchEngine.cs | 4 +++- src/Ombi.Core/Engine/TvSearchEngine.cs | 21 +++++++++++------ .../Engine/V2/MovieSearchEngineV2.cs | 23 ++++++++++++------- src/Ombi.Core/Engine/V2/TvSearchEngineV2.cs | 12 ++++++---- src/Ombi/ClientApp/src/typings/globals.d.ts | 1 - 5 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/Ombi.Core/Engine/MovieSearchEngine.cs b/src/Ombi.Core/Engine/MovieSearchEngine.cs index 4737c3992..cda8676fd 100644 --- a/src/Ombi.Core/Engine/MovieSearchEngine.cs +++ b/src/Ombi.Core/Engine/MovieSearchEngine.cs @@ -45,7 +45,9 @@ namespace Ombi.Core.Engine public async Task LookupImdbInformation(int theMovieDbId, string langCode = null) { langCode = await DefaultLanguageCode(langCode); - var movieInfo = await MovieApi.GetMovieInformationWithExtraInfo(theMovieDbId, langCode); + var movieInfo = await Cache.GetOrAdd(nameof(LookupImdbInformation) + langCode + theMovieDbId, + async () => await MovieApi.GetMovieInformationWithExtraInfo(theMovieDbId, langCode), + DateTime.Now.AddHours(12)); var viewMovie = Mapper.Map(movieInfo); return await ProcessSingleMovie(viewMovie, true); diff --git a/src/Ombi.Core/Engine/TvSearchEngine.cs b/src/Ombi.Core/Engine/TvSearchEngine.cs index 93e451f1d..f6e470dce 100644 --- a/src/Ombi.Core/Engine/TvSearchEngine.cs +++ b/src/Ombi.Core/Engine/TvSearchEngine.cs @@ -72,13 +72,16 @@ namespace Ombi.Core.Engine public async Task GetShowInformation(int tvdbid) { - var show = await TvMazeApi.ShowLookupByTheTvDbId(tvdbid); + var show = await Cache.GetOrAdd(nameof(GetShowInformation) + tvdbid, + async () => await TvMazeApi.ShowLookupByTheTvDbId(tvdbid), DateTime.Now.AddHours(12)); if (show == null) { // We don't have enough information return null; } - var episodes = await TvMazeApi.EpisodeLookup(show.id); + + var episodes = await Cache.GetOrAdd("TvMazeEpisodeLookup" + show.id, + async () => await TvMazeApi.EpisodeLookup(show.id), DateTime.Now.AddHours(12)); if (episodes == null || !episodes.Any()) { // We don't have enough information @@ -135,7 +138,8 @@ namespace Ombi.Core.Engine var results = new List(); foreach (var pagesToLoad in pages) { - var apiResult = await TraktApi.GetPopularShows(pagesToLoad.Page, ResultLimit); + var apiResult = await Cache.GetOrAdd(nameof(Popular) + pagesToLoad.Page, + async () => await TraktApi.GetPopularShows(pagesToLoad.Page, ResultLimit), DateTime.Now.AddHours(12)); results.AddRange(apiResult.Skip(pagesToLoad.Skip).Take(pagesToLoad.Take)); } var processed = ProcessResults(results); @@ -155,8 +159,9 @@ namespace Ombi.Core.Engine var pages = PaginationHelper.GetNextPages(currentlyLoaded, amountToLoad, ResultLimit); var results = new List(); foreach (var pagesToLoad in pages) - { - var apiResult = await TraktApi.GetAnticipatedShows(pagesToLoad.Page, ResultLimit); + { + var apiResult = await Cache.GetOrAdd(nameof(Anticipated) + pagesToLoad.Page, + async () => await TraktApi.GetAnticipatedShows(pagesToLoad.Page, ResultLimit), DateTime.Now.AddHours(12)); results.AddRange(apiResult.Skip(pagesToLoad.Skip).Take(pagesToLoad.Take)); } var processed = ProcessResults(results); @@ -183,7 +188,8 @@ namespace Ombi.Core.Engine var results = new List(); foreach (var pagesToLoad in pages) { - var apiResult = await TraktApi.GetMostWatchesShows(null, pagesToLoad.Page, ResultLimit); + var apiResult = await Cache.GetOrAdd(nameof(MostWatches) + pagesToLoad.Page, + async () => await TraktApi.GetMostWatchesShows(null, pagesToLoad.Page, ResultLimit), DateTime.Now.AddHours(12)); results.AddRange(apiResult.Skip(pagesToLoad.Skip).Take(pagesToLoad.Take)); } var processed = ProcessResults(results); @@ -196,7 +202,8 @@ namespace Ombi.Core.Engine var results = new List(); foreach (var pagesToLoad in pages) { - var apiResult = await TraktApi.GetTrendingShows(pagesToLoad.Page, ResultLimit); + var apiResult = await Cache.GetOrAdd(nameof(Trending) + pagesToLoad.Page, + async () => await TraktApi.GetTrendingShows(pagesToLoad.Page, ResultLimit), DateTime.Now.AddHours(12)); results.AddRange(apiResult.Skip(pagesToLoad.Skip).Take(pagesToLoad.Take)); } var processed = ProcessResults(results); diff --git a/src/Ombi.Core/Engine/V2/MovieSearchEngineV2.cs b/src/Ombi.Core/Engine/V2/MovieSearchEngineV2.cs index cb3162900..a6f185eb3 100644 --- a/src/Ombi.Core/Engine/V2/MovieSearchEngineV2.cs +++ b/src/Ombi.Core/Engine/V2/MovieSearchEngineV2.cs @@ -41,7 +41,8 @@ namespace Ombi.Core.Engine.V2 public async Task GetFullMovieInformation(int theMovieDbId, string langCode = null) { langCode = await DefaultLanguageCode(langCode); - var movieInfo = await MovieApi.GetFullMovieInfo(theMovieDbId, langCode); + var movieInfo = await Cache.GetOrAdd(nameof(GetFullMovieInformation) + theMovieDbId + langCode, + async () => await MovieApi.GetFullMovieInfo(theMovieDbId, langCode), DateTime.Now.AddHours(12)); return await ProcessSingleMovie(movieInfo); } @@ -49,7 +50,8 @@ namespace Ombi.Core.Engine.V2 public async Task GetCollection(int collectionId, string langCode = null) { langCode = await DefaultLanguageCode(langCode); - var collections = await MovieApi.GetCollection(langCode, collectionId); + var collections = await Cache.GetOrAdd(nameof(GetCollection) + collectionId + langCode, + async () => await MovieApi.GetCollection(langCode, collectionId), DateTime.Now.AddDays(1)); var c = await ProcessCollection(collections); c.Collection = c.Collection.OrderBy(x => x.ReleaseDate).ToList(); @@ -108,13 +110,14 @@ namespace Ombi.Core.Engine.V2 public async Task> PopularMovies(int currentlyLoaded, int toLoad) { var langCode = await DefaultLanguageCode(null); - + var pages = PaginationHelper.GetNextPages(currentlyLoaded, toLoad, _theMovieDbMaxPageItems); var results = new List(); foreach (var pagesToLoad in pages) { - var apiResult = await MovieApi.PopularMovies(langCode, pagesToLoad.Page); + var apiResult = await Cache.GetOrAdd(nameof(PopularMovies) + pagesToLoad.Page + langCode, + async () => await MovieApi.PopularMovies(langCode, pagesToLoad.Page), DateTime.Now.AddHours(12)); results.AddRange(apiResult.Skip(pagesToLoad.Skip).Take(pagesToLoad.Take)); } return await TransformMovieResultsToResponse(results); @@ -147,7 +150,8 @@ namespace Ombi.Core.Engine.V2 var results = new List(); foreach (var pagesToLoad in pages) { - var apiResult = await MovieApi.TopRated(langCode, pagesToLoad.Page); + var apiResult = await Cache.GetOrAdd(nameof(TopRatedMovies) + pagesToLoad.Page + langCode, + async () => await MovieApi.TopRated(langCode, pagesToLoad.Page), DateTime.Now.AddHours(12)); results.AddRange(apiResult.Skip(pagesToLoad.Skip).Take(pagesToLoad.Take)); } return await TransformMovieResultsToResponse(results); @@ -162,7 +166,8 @@ namespace Ombi.Core.Engine.V2 var results = new List(); foreach (var pagesToLoad in pages) { - var apiResult = await MovieApi.NowPlaying(langCode, pagesToLoad.Page); + var apiResult = await Cache.GetOrAdd(nameof(NowPlayingMovies) + pagesToLoad.Page + langCode, + async () => await MovieApi.NowPlaying(langCode, pagesToLoad.Page), DateTime.Now.AddHours(12)); results.AddRange(apiResult.Skip(pagesToLoad.Skip).Take(pagesToLoad.Take)); } return await TransformMovieResultsToResponse(results); @@ -197,7 +202,8 @@ namespace Ombi.Core.Engine.V2 var results = new List(); foreach (var pagesToLoad in pages) { - var apiResult = await MovieApi.Upcoming(langCode, pagesToLoad.Page); + var apiResult = await Cache.GetOrAdd(nameof(UpcomingMovies) + pagesToLoad.Page + langCode, + async () => await MovieApi.Upcoming(langCode, pagesToLoad.Page), DateTime.Now.AddHours(12)); results.AddRange(apiResult.Skip(pagesToLoad.Skip).Take(pagesToLoad.Take)); } return await TransformMovieResultsToResponse(results); @@ -287,7 +293,8 @@ namespace Ombi.Core.Engine.V2 { if (viewMovie.ImdbId.IsNullOrEmpty()) { - var showInfo = await MovieApi.GetMovieInformation(viewMovie.Id); + var showInfo = await Cache.GetOrAdd("GetMovieInformationWIthImdbId" + viewMovie.Id, + async () => await MovieApi.GetMovieInformation(viewMovie.Id), DateTime.Now.AddHours(12)); viewMovie.Id = showInfo.Id; // TheMovieDbId viewMovie.ImdbId = showInfo.ImdbId; } diff --git a/src/Ombi.Core/Engine/V2/TvSearchEngineV2.cs b/src/Ombi.Core/Engine/V2/TvSearchEngineV2.cs index 4dd20926b..7a423db05 100644 --- a/src/Ombi.Core/Engine/V2/TvSearchEngineV2.cs +++ b/src/Ombi.Core/Engine/V2/TvSearchEngineV2.cs @@ -1,4 +1,5 @@ -using AutoMapper; +using System; +using AutoMapper; using System.Collections.Generic; using System.Linq; @@ -50,8 +51,10 @@ namespace Ombi.Core.Engine.V2 public async Task GetShowInformation(int tvdbid) { - var tvdbshow = await TvMazeApi.ShowLookupByTheTvDbId(tvdbid); - var show = await TvMazeApi.GetTvFullInformation(tvdbshow.id); + var tvdbshow = await Cache.GetOrAdd(nameof(GetShowInformation) + tvdbid, + async () => await TvMazeApi.ShowLookupByTheTvDbId(tvdbid), DateTime.Now.AddHours(12)); + var show = await Cache.GetOrAdd("GetTvFullInformation" + tvdbshow.id, + async () => await TvMazeApi.GetTvFullInformation(tvdbshow.id), DateTime.Now.AddHours(12)); if (show == null) { // We don't have enough information @@ -62,7 +65,8 @@ namespace Ombi.Core.Engine.V2 Task traktInfoTask = new Task(() => null); if (show.externals?.imdb.HasValue() ?? false) { - traktInfoTask = TraktApi.GetTvExtendedInfo(show.externals?.imdb); + traktInfoTask = Cache.GetOrAdd("GetExtendedTvInfoTrakt" + show.externals?.imdb, + () => TraktApi.GetTvExtendedInfo(show.externals?.imdb), DateTime.Now.AddHours(12)); } var mapped = Mapper.Map(show); diff --git a/src/Ombi/ClientApp/src/typings/globals.d.ts b/src/Ombi/ClientApp/src/typings/globals.d.ts index ff6860d41..85fd9e2a0 100644 --- a/src/Ombi/ClientApp/src/typings/globals.d.ts +++ b/src/Ombi/ClientApp/src/typings/globals.d.ts @@ -1,6 +1,5 @@ // Globals declare var __webpack_public_path__: any; -declare var module: any; // declare module "*.json" { // const value: any;