#114 start caching quality profiles. Set the cache on startup and when obtaining quality profiles in settings

pull/140/head
Drewster727 9 years ago
parent 08b14d0164
commit e8c222f66e

@ -29,5 +29,8 @@ namespace PlexRequests.Core
public class CacheKeys public class CacheKeys
{ {
public const string TvDbToken = "TheTvDbApiToken"; public const string TvDbToken = "TheTvDbApiToken";
public const string SonarrQualityProfiles = "SonarrQualityProfiles";
public const string SickRageQualityProfiles = "SickRageQualityProfiles";
public const string CouchPotatoQualityProfiles = "CouchPotatoQualityProfiles";
} }
} }

@ -37,6 +37,7 @@ using PlexRequests.Helpers;
using PlexRequests.Store; using PlexRequests.Store;
using PlexRequests.Store.Repository; using PlexRequests.Store.Repository;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace PlexRequests.Core namespace PlexRequests.Core
{ {
@ -115,6 +116,52 @@ namespace PlexRequests.Core
s.SaveSettings(defaultSettings); s.SaveSettings(defaultSettings);
} }
public async void CacheQualityProfiles()
{
var mc = new MemoryCacheProvider();
try
{
CacheSonarrQualityProfiles(mc);
CacheCouchPotatoQualityProfiles(mc);
// we don't need to cache sickrage profiles, those are static
// TODO: cache headphones profiles?
}
catch (Exception)
{
Log.Error("Failed to cache quality profiles on startup!");
}
}
private async void CacheSonarrQualityProfiles(MemoryCacheProvider cacheProvider)
{
var sonarrSettingsService = new SettingsServiceV2<SonarrSettings>(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider()));
var sonarrSettings = sonarrSettingsService.GetSettings();
if (sonarrSettings.Enabled) {
cacheProvider.GetOrSet(CacheKeys.SonarrQualityProfiles, () =>
{
SonarrApi sonarrApi = new SonarrApi();
return sonarrApi.GetProfiles(sonarrSettings.ApiKey, sonarrSettings.FullUri);
});
}
}
private async void CacheCouchPotatoQualityProfiles(MemoryCacheProvider cacheProvider)
{
var cpSettingsService = new SettingsServiceV2<CouchPotatoSettings>(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider()));
var cpSettings = cpSettingsService.GetSettings();
if (cpSettings.Enabled)
{
cacheProvider.GetOrSet(CacheKeys.CouchPotatoQualityProfiles, () =>
{
CouchPotatoApi cpApi = new CouchPotatoApi();
return cpApi.GetProfiles(cpSettings.FullUri, cpSettings.ApiKey);
});
}
}
private void MigrateDbFrom1300() // TODO: Remove in v1.7 private void MigrateDbFrom1300() // TODO: Remove in v1.7
{ {

@ -83,7 +83,7 @@ namespace PlexRequests.Helpers
/// <param name="key">The key.</param> /// <param name="key">The key.</param>
/// <param name="data">The object we want to store.</param> /// <param name="data">The object we want to store.</param>
/// <param name="cacheTime">The amount of time we want to cache the object.</param> /// <param name="cacheTime">The amount of time we want to cache the object.</param>
public void Set(string key, object data, int cacheTime) 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) };
Cache.Add(new CacheItem(key, data), policy); Cache.Add(new CacheItem(key, data), policy);

@ -374,6 +374,13 @@ namespace PlexRequests.UI.Modules
var settings = this.Bind<SonarrSettings>(); var settings = this.Bind<SonarrSettings>();
var profiles = SonarrApi.GetProfiles(settings.ApiKey, settings.FullUri); var profiles = SonarrApi.GetProfiles(settings.ApiKey, settings.FullUri);
// set the cache
if (profiles != null)
{
var cache = new MemoryCacheProvider();
cache.Set(CacheKeys.SonarrQualityProfiles, profiles);
}
return Response.AsJson(profiles); return Response.AsJson(profiles);
} }
@ -582,6 +589,13 @@ namespace PlexRequests.UI.Modules
var settings = this.Bind<CouchPotatoSettings>(); var settings = this.Bind<CouchPotatoSettings>();
var profiles = CpApi.GetProfiles(settings.FullUri, settings.ApiKey); var profiles = CpApi.GetProfiles(settings.FullUri, settings.ApiKey);
// set the cache
if (profiles != null)
{
var cache = new MemoryCacheProvider();
cache.Set(CacheKeys.CouchPotatoQualityProfiles, profiles);
}
return Response.AsJson(profiles); return Response.AsJson(profiles);
} }

@ -126,7 +126,7 @@ namespace PlexRequests.UI.Modules
})); }));
var mc = new MemoryCacheProvider();
List<QualityModel> qualities = new List<QualityModel>(); List<QualityModel> qualities = new List<QualityModel>();
if (isAdmin) if (isAdmin)
@ -136,10 +136,13 @@ namespace PlexRequests.UI.Modules
{ {
taskList.Add(Task.Factory.StartNew(() => taskList.Add(Task.Factory.StartNew(() =>
{ {
return CpApi.GetProfiles(cpSettings.FullUri, cpSettings.ApiKey).list.Select(x => new QualityModel() { Id = x._id, Name = x.label }); // TODO: cache this! return mc.GetOrSet(CacheKeys.CouchPotatoQualityProfiles, () =>
{
return CpApi.GetProfiles(cpSettings.FullUri, cpSettings.ApiKey); // TODO: cache this!
});
}).ContinueWith((t) => }).ContinueWith((t) =>
{ {
qualities = t.Result.ToList(); qualities = t.Result.list.Select(x => new QualityModel() { Id = x._id, Name = x.label }).ToList();
})); }));
} }
} }
@ -199,6 +202,7 @@ namespace PlexRequests.UI.Modules
} }
})); }));
var mc = new MemoryCacheProvider();
List<QualityModel> qualities = new List<QualityModel>(); List<QualityModel> qualities = new List<QualityModel>();
if (isAdmin) if (isAdmin)
{ {
@ -207,25 +211,22 @@ namespace PlexRequests.UI.Modules
{ {
taskList.Add(Task.Factory.StartNew(() => taskList.Add(Task.Factory.StartNew(() =>
{ {
return SonarrApi.GetProfiles(sonarrSettings.ApiKey, sonarrSettings.FullUri).Select(x => new QualityModel() { Id = x.id.ToString(), Name = x.name }); // TODO: cache this! return mc.GetOrSet(CacheKeys.SonarrQualityProfiles, () =>
{
return SonarrApi.GetProfiles(sonarrSettings.ApiKey, sonarrSettings.FullUri); // TODO: cache this!
});
}).ContinueWith((t) => }).ContinueWith((t) =>
{ {
qualities = t.Result.ToList(); qualities = t.Result.Select(x => new QualityModel() { Id = x.id.ToString(), Name = x.name }).ToList();
})); }));
} }
else { else {
var sickRageSettings = SickRageSettings.GetSettings(); var sickRageSettings = SickRageSettings.GetSettings();
if (sickRageSettings.Enabled) if (sickRageSettings.Enabled)
{ {
taskList.Add(Task.Factory.StartNew(() => qualities = sickRageSettings.Qualities.Select(x => new QualityModel() { Id = x.Key, Name = x.Value }).ToList();
{
return sickRageSettings.Qualities.Select(x => new QualityModel() { Id = x.Key, Name = x.Value }); // TODO: cache this!
}).ContinueWith((t) =>
{
qualities = t.Result.ToList();
}));
} }
} }
} }

@ -63,8 +63,10 @@ namespace PlexRequests.UI
WriteOutVersion(); WriteOutVersion();
var s = new Setup(); var s = new Setup();
s.CacheQualityProfiles();
var cn = s.SetupDb(); var cn = s.SetupDb();
ConfigureTargets(cn); ConfigureTargets(cn);
if (port == -1) if (port == -1)
port = GetStartupPort(); port = GetStartupPort();

Loading…
Cancel
Save