Some small tweaks to improve the memory alloc

This commit is contained in:
tidusjar 2016-08-04 21:53:19 +01:00
parent 5d18877b49
commit 7f6c70d7b2
5 changed files with 147 additions and 150 deletions

View file

@ -34,6 +34,7 @@ namespace PlexRequests.Helpers
public class MemoryCacheProvider : ICacheProvider public class MemoryCacheProvider : ICacheProvider
{ {
private ObjectCache Cache => MemoryCache.Default; private ObjectCache Cache => MemoryCache.Default;
private readonly object _lock = new object();
/// <summary> /// <summary>
/// Gets the item from the cache, if the item is not present /// Gets the item from the cache, if the item is not present
@ -91,8 +92,10 @@ 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
{ {
lock (key) lock (_lock)
{
return Cache.Get(key) as T; return Cache.Get(key) as T;
}
} }
/// <summary> /// <summary>
@ -104,7 +107,7 @@ 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) lock (_lock)
{ {
Cache.Remove(key); Cache.Remove(key);
Cache.Add(new CacheItem(key, data), policy); Cache.Add(new CacheItem(key, data), policy);
@ -120,7 +123,7 @@ 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) lock (_lock)
{ {
Cache.Remove(k.Key); Cache.Remove(k.Key);
} }

View file

@ -43,7 +43,7 @@ namespace PlexRequests.Services.Interfaces
/// Gets the episode's stored in the cache. /// Gets the episode's stored in the cache.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
IEnumerable<PlexEpisodeModel> GetEpisodeCache(); HashSet<PlexEpisodeModel> GetEpisodeCache();
/// <summary> /// <summary>
/// Gets the episode's stored in the cache and then filters on the TheTvDBId. /// Gets the episode's stored in the cache and then filters on the TheTvDBId.
/// </summary> /// </summary>

View file

@ -241,7 +241,7 @@ namespace PlexRequests.Services.Jobs
public bool IsEpisodeAvailable(string theTvDbId, int season, int episode) public bool IsEpisodeAvailable(string theTvDbId, int season, int episode)
{ {
var episodes = Cache.Get<List<PlexEpisodeModel>>(CacheKeys.PlexEpisodes); var episodes = Cache.Get<HashSet<PlexEpisodeModel>>(CacheKeys.PlexEpisodes);
if (episodes == null) if (episodes == null)
{ {
Log.Info("Episode cache info is not available. tvdbid: {0}, season: {1}, episode: {2}",theTvDbId, season, episode); Log.Info("Episode cache info is not available. tvdbid: {0}, season: {1}, episode: {2}",theTvDbId, season, episode);
@ -261,13 +261,13 @@ namespace PlexRequests.Services.Jobs
/// Gets the episode's stored in the cache. /// Gets the episode's stored in the cache.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public IEnumerable<PlexEpisodeModel> GetEpisodeCache() public HashSet<PlexEpisodeModel> GetEpisodeCache()
{ {
var episodes = Cache.Get<List<PlexEpisodeModel>>(CacheKeys.PlexEpisodes); var episodes = Cache.Get<HashSet<PlexEpisodeModel>>(CacheKeys.PlexEpisodes);
if (episodes == null) if (episodes == null)
{ {
Log.Info("Episode cache info is not available."); Log.Info("Episode cache info is not available.");
return new List<PlexEpisodeModel>(); return new HashSet<PlexEpisodeModel>();
} }
return episodes; return episodes;
} }

View file

@ -65,6 +65,7 @@ namespace PlexRequests.Services.Jobs
public void CacheEpisodes() public void CacheEpisodes()
{ {
var results = new PlexSearch(); var results = new PlexSearch();
var videoHashset = new HashSet<Video>();
var settings = Plex.GetSettings(); var settings = Plex.GetSettings();
if (string.IsNullOrEmpty(settings.PlexAuthToken)) if (string.IsNullOrEmpty(settings.PlexAuthToken))
{ {
@ -85,32 +86,26 @@ namespace PlexRequests.Services.Jobs
currentPosition += ResultCount; currentPosition += ResultCount;
while (currentPosition < totalSize) while (currentPosition < totalSize)
{ {
results.Video.AddRange(PlexApi.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, tvSectionId, currentPosition, ResultCount).Video); videoHashset.UnionWith(PlexApi.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, tvSectionId, currentPosition, ResultCount).Video
.Where(x => x.Type.Equals(PlexType, StringComparison.InvariantCultureIgnoreCase)));
currentPosition += ResultCount; currentPosition += ResultCount;
} }
var filteredList = results.Video.Where(x => x.Type.Equals(PlexType, StringComparison.InvariantCultureIgnoreCase)); var episodesModel = new HashSet<PlexEpisodeModel>();
var episodesModel = new List<PlexEpisodeModel>();
var metadataList = new List<PlexEpisodeMetadata>();
foreach (var video in filteredList) foreach (var video in videoHashset)
{ {
var ratingKey = video.RatingKey; var ratingKey = video.RatingKey;
var metadata = PlexApi.GetEpisodeMetaData(settings.PlexAuthToken, settings.FullUri, ratingKey); var metadata = PlexApi.GetEpisodeMetaData(settings.PlexAuthToken, settings.FullUri, ratingKey);
metadataList.Add(metadata);
}
foreach (var metadataVideo in metadata.Video)
foreach (var m in metadataList)
{
foreach (var video in m.Video)
{ {
episodesModel.Add(new PlexEpisodeModel episodesModel.Add(new PlexEpisodeModel
{ {
RatingKey = video.RatingKey, RatingKey = metadataVideo.RatingKey,
EpisodeTitle = video.Title, EpisodeTitle = metadataVideo.Title,
Guid = video.Guid, Guid = metadataVideo.Guid,
ShowTitle = video.GrandparentTitle ShowTitle = metadataVideo.GrandparentTitle
}); });
} }
} }

View file

@ -130,7 +130,6 @@
generateNotify(response.message, "success"); generateNotify(response.message, "success");
$('#spinner').attr("class", "fa fa-check"); $('#spinner').attr("class", "fa fa-check");
$('#authToken').val(response.authToken);
} else { } else {
generateNotify(response.message, "warning"); generateNotify(response.message, "warning");
$('#spinner').attr("class", "fa fa-times"); $('#spinner').attr("class", "fa fa-times");