fix the cacher by adding locking + extra logging in the plex checker + use a const key for scheduler caching time

pull/193/head
Drewster727 9 years ago
parent 9c61f909de
commit 121465210d

@ -28,6 +28,11 @@ namespace PlexRequests.Core
{ {
public class CacheKeys public class CacheKeys
{ {
public struct TimeFrameMinutes
{
public const int SchedulerCaching = 10;
}
public const string PlexLibaries = "PlexLibaries"; public const string PlexLibaries = "PlexLibaries";
public const string TvDbToken = "TheTvDbApiToken"; public const string TvDbToken = "TheTvDbApiToken";

@ -73,8 +73,8 @@ namespace PlexRequests.Helpers
/// <returns></returns> /// <returns></returns>
public T Get<T>(string key) where T : class public T Get<T>(string key) where T : class
{ {
var item = Cache.Get(key) as T; lock (key)
return item; return Cache.Get(key) as T;
} }
/// <summary> /// <summary>
@ -86,8 +86,12 @@ namespace PlexRequests.Helpers
public void Set(string key, object data, int cacheTime = 20) public void Set(string key, object data, int cacheTime = 20)
{ {
var policy = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime) }; var policy = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime) };
lock (key)
{
Cache.Remove(key);
Cache.Add(new CacheItem(key, data), policy); Cache.Add(new CacheItem(key, data), policy);
} }
}
/// <summary> /// <summary>
/// Removes the specified object from the cache. /// Removes the specified object from the cache.
@ -97,9 +101,12 @@ namespace PlexRequests.Helpers
{ {
var keys = Cache.Where(x => x.Key.Contains(key)); var keys = Cache.Where(x => x.Key.Contains(key));
foreach (var k in keys) foreach (var k in keys)
{
lock (key)
{ {
Cache.Remove(k.Key); Cache.Remove(k.Key);
} }
} }
} }
}
} }

@ -63,7 +63,7 @@ namespace PlexRequests.Services.Jobs
{ {
Log.Trace("Getting all movies from CouchPotato"); Log.Trace("Getting all movies from CouchPotato");
var movies = CpApi.GetMovies(settings.FullUri, settings.ApiKey, new[] { "active" }); var movies = CpApi.GetMovies(settings.FullUri, settings.ApiKey, new[] { "active" });
Cache.Set(CacheKeys.CouchPotatoQueued, movies, 10); Cache.Set(CacheKeys.CouchPotatoQueued, movies, CacheKeys.TimeFrameMinutes.SchedulerCaching);
} }
} }

@ -240,7 +240,7 @@ namespace PlexRequests.Services.Jobs
private List<PlexSearch> CachedLibraries(AuthenticationSettings authSettings, PlexSettings plexSettings, bool setCache) private List<PlexSearch> 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<PlexSearch> results = new List<PlexSearch>(); List<PlexSearch> results = new List<PlexSearch>();
@ -252,14 +252,19 @@ namespace PlexRequests.Services.Jobs
if (setCache) if (setCache)
{ {
Log.Trace("Plex Lib API Call");
results = GetLibraries(authSettings, plexSettings); results = GetLibraries(authSettings, plexSettings);
Cache.Set(CacheKeys.PlexLibaries, results, 10);
Log.Trace("Plex Lib Cache Set Call");
Cache.Set(CacheKeys.PlexLibaries, results, CacheKeys.TimeFrameMinutes.SchedulerCaching);
} }
else else
{ {
Log.Trace("Plex Lib GetSet Call");
results = Cache.GetOrSet(CacheKeys.PlexLibaries, () => { results = Cache.GetOrSet(CacheKeys.PlexLibaries, () => {
Log.Trace("Plex Lib API Call (inside getset)");
return GetLibraries(authSettings, plexSettings); return GetLibraries(authSettings, plexSettings);
}, 10); }, CacheKeys.TimeFrameMinutes.SchedulerCaching);
} }
return results; return results;
} }
@ -282,6 +287,7 @@ namespace PlexRequests.Services.Jobs
} }
} }
Log.Trace("Returning Plex Libs");
return libs; return libs;
} }

@ -63,7 +63,7 @@ namespace PlexRequests.Services.Jobs
{ {
Log.Trace("Getting all shows from SickRage"); Log.Trace("Getting all shows from SickRage");
var movies = SrApi.GetShows(settings.ApiKey, settings.FullUri); var movies = SrApi.GetShows(settings.ApiKey, settings.FullUri);
Cache.Set(CacheKeys.SickRageQueued, movies.Result); Cache.Set(CacheKeys.SickRageQueued, movies.Result, CacheKeys.TimeFrameMinutes.SchedulerCaching);
} }
} }

@ -64,7 +64,7 @@ namespace PlexRequests.Services.Jobs
{ {
Log.Trace("Getting all tv series from Sonarr"); Log.Trace("Getting all tv series from Sonarr");
var series = SonarrApi.GetSeries(settings.ApiKey, settings.FullUri); var series = SonarrApi.GetSeries(settings.ApiKey, settings.FullUri);
Cache.Set(CacheKeys.SonarrQueued, series, 10); Cache.Set(CacheKeys.SonarrQueued, series, CacheKeys.TimeFrameMinutes.SchedulerCaching);
} }
} }

Loading…
Cancel
Save