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 struct TimeFrameMinutes
{
public const int SchedulerCaching = 10;
}
public const string PlexLibaries = "PlexLibaries";
public const string TvDbToken = "TheTvDbApiToken";

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

@ -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);
}
}

@ -240,7 +240,7 @@ namespace PlexRequests.Services.Jobs
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>();
@ -252,14 +252,19 @@ namespace PlexRequests.Services.Jobs
if (setCache)
{
Log.Trace("Plex Lib API Call");
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
{
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;
}

@ -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);
}
}

@ -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);
}
}

Loading…
Cancel
Save