From 121465210d58a53210eec8cf10c4960349fa3736 Mon Sep 17 00:00:00 2001 From: Drewster727 Date: Tue, 19 Apr 2016 13:41:56 -0500 Subject: [PATCH 1/2] fix the cacher by adding locking + extra logging in the plex checker + use a const key for scheduler caching time --- PlexRequests.Core/CacheKeys.cs | 5 +++++ PlexRequests.Helpers/MemoryCacheProvider.cs | 15 +++++++++++---- PlexRequests.Services/Jobs/CouchPotatoCacher.cs | 2 +- .../Jobs/PlexAvailabilityChecker.cs | 14 ++++++++++---- PlexRequests.Services/Jobs/SickRageCacher.cs | 2 +- PlexRequests.Services/Jobs/SonarrCacher.cs | 2 +- 6 files changed, 29 insertions(+), 11 deletions(-) diff --git a/PlexRequests.Core/CacheKeys.cs b/PlexRequests.Core/CacheKeys.cs index d10bb0bb2..52994db1c 100644 --- a/PlexRequests.Core/CacheKeys.cs +++ b/PlexRequests.Core/CacheKeys.cs @@ -28,6 +28,11 @@ namespace PlexRequests.Core { public class CacheKeys { + public struct TimeFrameMinutes + { + public const int SchedulerCaching = 10; + } + public const string PlexLibaries = "PlexLibaries"; public const string TvDbToken = "TheTvDbApiToken"; diff --git a/PlexRequests.Helpers/MemoryCacheProvider.cs b/PlexRequests.Helpers/MemoryCacheProvider.cs index 6e513502c..fdb78f291 100644 --- a/PlexRequests.Helpers/MemoryCacheProvider.cs +++ b/PlexRequests.Helpers/MemoryCacheProvider.cs @@ -73,8 +73,8 @@ namespace PlexRequests.Helpers /// public T Get(string key) where T : class { - var item = Cache.Get(key) as T; - return item; + lock (key) + return Cache.Get(key) as T; } /// @@ -86,7 +86,11 @@ namespace PlexRequests.Helpers public void Set(string key, object data, int cacheTime = 20) { var policy = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime) }; - Cache.Add(new CacheItem(key, data), policy); + lock (key) + { + Cache.Remove(key); + Cache.Add(new CacheItem(key, data), policy); + } } /// @@ -98,7 +102,10 @@ namespace PlexRequests.Helpers var keys = Cache.Where(x => x.Key.Contains(key)); foreach (var k in keys) { - Cache.Remove(k.Key); + lock (key) + { + Cache.Remove(k.Key); + } } } } diff --git a/PlexRequests.Services/Jobs/CouchPotatoCacher.cs b/PlexRequests.Services/Jobs/CouchPotatoCacher.cs index 8bc50c5b6..66723036a 100644 --- a/PlexRequests.Services/Jobs/CouchPotatoCacher.cs +++ b/PlexRequests.Services/Jobs/CouchPotatoCacher.cs @@ -63,7 +63,7 @@ namespace PlexRequests.Services.Jobs { Log.Trace("Getting all movies from CouchPotato"); var movies = CpApi.GetMovies(settings.FullUri, settings.ApiKey, new[] { "active" }); - Cache.Set(CacheKeys.CouchPotatoQueued, movies, 10); + Cache.Set(CacheKeys.CouchPotatoQueued, movies, CacheKeys.TimeFrameMinutes.SchedulerCaching); } } diff --git a/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs b/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs index bb8f957a0..f1288ec55 100644 --- a/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs +++ b/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs @@ -240,7 +240,7 @@ namespace PlexRequests.Services.Jobs private List CachedLibraries(AuthenticationSettings authSettings, PlexSettings plexSettings, bool setCache) { - Log.Trace("Obtaining library sections from Plex for the following request"); + Log.Trace("Obtaining library sections from Plex"); List results = new List(); @@ -252,14 +252,19 @@ namespace PlexRequests.Services.Jobs if (setCache) { - results = GetLibraries(authSettings, plexSettings); - Cache.Set(CacheKeys.PlexLibaries, results, 10); + Log.Trace("Plex Lib API Call"); + results = GetLibraries(authSettings, plexSettings); + + Log.Trace("Plex Lib Cache Set Call"); + Cache.Set(CacheKeys.PlexLibaries, results, CacheKeys.TimeFrameMinutes.SchedulerCaching); } else { + Log.Trace("Plex Lib GetSet Call"); results = Cache.GetOrSet(CacheKeys.PlexLibaries, () => { + Log.Trace("Plex Lib API Call (inside getset)"); return GetLibraries(authSettings, plexSettings); - }, 10); + }, CacheKeys.TimeFrameMinutes.SchedulerCaching); } return results; } @@ -282,6 +287,7 @@ namespace PlexRequests.Services.Jobs } } + Log.Trace("Returning Plex Libs"); return libs; } diff --git a/PlexRequests.Services/Jobs/SickRageCacher.cs b/PlexRequests.Services/Jobs/SickRageCacher.cs index 3c626a336..8b394eec3 100644 --- a/PlexRequests.Services/Jobs/SickRageCacher.cs +++ b/PlexRequests.Services/Jobs/SickRageCacher.cs @@ -63,7 +63,7 @@ namespace PlexRequests.Services.Jobs { Log.Trace("Getting all shows from SickRage"); var movies = SrApi.GetShows(settings.ApiKey, settings.FullUri); - Cache.Set(CacheKeys.SickRageQueued, movies.Result); + Cache.Set(CacheKeys.SickRageQueued, movies.Result, CacheKeys.TimeFrameMinutes.SchedulerCaching); } } diff --git a/PlexRequests.Services/Jobs/SonarrCacher.cs b/PlexRequests.Services/Jobs/SonarrCacher.cs index 63e2425d1..50b238735 100644 --- a/PlexRequests.Services/Jobs/SonarrCacher.cs +++ b/PlexRequests.Services/Jobs/SonarrCacher.cs @@ -64,7 +64,7 @@ namespace PlexRequests.Services.Jobs { Log.Trace("Getting all tv series from Sonarr"); var series = SonarrApi.GetSeries(settings.ApiKey, settings.FullUri); - Cache.Set(CacheKeys.SonarrQueued, series, 10); + Cache.Set(CacheKeys.SonarrQueued, series, CacheKeys.TimeFrameMinutes.SchedulerCaching); } } From 508baeec04bb3a810f4bfbc565e3dc1914f10cf8 Mon Sep 17 00:00:00 2001 From: Drewster727 Date: Tue, 19 Apr 2016 13:55:37 -0500 Subject: [PATCH 2/2] increase the scheduler cache timeframe to avoid losing cache when the remote api endpoints go offline (due to a reboot or some other reason) -- if they're online, the cache will get refreshed every 10 minutes like normal --- PlexRequests.Core/CacheKeys.cs | 2 +- PlexRequests.Services/Jobs/CouchPotatoCacher.cs | 5 ++++- PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs | 5 ++++- PlexRequests.Services/Jobs/SickRageCacher.cs | 7 +++++-- PlexRequests.Services/Jobs/SonarrCacher.cs | 5 ++++- 5 files changed, 18 insertions(+), 6 deletions(-) diff --git a/PlexRequests.Core/CacheKeys.cs b/PlexRequests.Core/CacheKeys.cs index 52994db1c..439953d94 100644 --- a/PlexRequests.Core/CacheKeys.cs +++ b/PlexRequests.Core/CacheKeys.cs @@ -30,7 +30,7 @@ namespace PlexRequests.Core { public struct TimeFrameMinutes { - public const int SchedulerCaching = 10; + public const int SchedulerCaching = 60; } public const string PlexLibaries = "PlexLibaries"; diff --git a/PlexRequests.Services/Jobs/CouchPotatoCacher.cs b/PlexRequests.Services/Jobs/CouchPotatoCacher.cs index 66723036a..2440477f0 100644 --- a/PlexRequests.Services/Jobs/CouchPotatoCacher.cs +++ b/PlexRequests.Services/Jobs/CouchPotatoCacher.cs @@ -63,7 +63,10 @@ namespace PlexRequests.Services.Jobs { Log.Trace("Getting all movies from CouchPotato"); var movies = CpApi.GetMovies(settings.FullUri, settings.ApiKey, new[] { "active" }); - Cache.Set(CacheKeys.CouchPotatoQueued, movies, CacheKeys.TimeFrameMinutes.SchedulerCaching); + if (movies != null) + { + Cache.Set(CacheKeys.CouchPotatoQueued, movies, CacheKeys.TimeFrameMinutes.SchedulerCaching); + } } } diff --git a/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs b/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs index f1288ec55..401b01a9c 100644 --- a/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs +++ b/PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs @@ -256,7 +256,10 @@ namespace PlexRequests.Services.Jobs results = GetLibraries(authSettings, plexSettings); Log.Trace("Plex Lib Cache Set Call"); - Cache.Set(CacheKeys.PlexLibaries, results, CacheKeys.TimeFrameMinutes.SchedulerCaching); + if (results != null) + { + Cache.Set(CacheKeys.PlexLibaries, results, CacheKeys.TimeFrameMinutes.SchedulerCaching); + } } else { diff --git a/PlexRequests.Services/Jobs/SickRageCacher.cs b/PlexRequests.Services/Jobs/SickRageCacher.cs index 8b394eec3..1a28956a5 100644 --- a/PlexRequests.Services/Jobs/SickRageCacher.cs +++ b/PlexRequests.Services/Jobs/SickRageCacher.cs @@ -62,8 +62,11 @@ namespace PlexRequests.Services.Jobs if (settings.Enabled) { Log.Trace("Getting all shows from SickRage"); - var movies = SrApi.GetShows(settings.ApiKey, settings.FullUri); - Cache.Set(CacheKeys.SickRageQueued, movies.Result, CacheKeys.TimeFrameMinutes.SchedulerCaching); + var shows = SrApi.GetShows(settings.ApiKey, settings.FullUri); + if (shows != null) + { + Cache.Set(CacheKeys.SickRageQueued, shows.Result, CacheKeys.TimeFrameMinutes.SchedulerCaching); + } } } diff --git a/PlexRequests.Services/Jobs/SonarrCacher.cs b/PlexRequests.Services/Jobs/SonarrCacher.cs index 50b238735..983a50eea 100644 --- a/PlexRequests.Services/Jobs/SonarrCacher.cs +++ b/PlexRequests.Services/Jobs/SonarrCacher.cs @@ -64,7 +64,10 @@ namespace PlexRequests.Services.Jobs { Log.Trace("Getting all tv series from Sonarr"); var series = SonarrApi.GetSeries(settings.ApiKey, settings.FullUri); - Cache.Set(CacheKeys.SonarrQueued, series, CacheKeys.TimeFrameMinutes.SchedulerCaching); + if (series != null) + { + Cache.Set(CacheKeys.SonarrQueued, series, CacheKeys.TimeFrameMinutes.SchedulerCaching); + } } }