Some small tweaks to improve the memory alloc

pull/470/head
tidusjar 9 years ago
parent 5d18877b49
commit 7f6c70d7b2

@ -34,6 +34,7 @@ namespace PlexRequests.Helpers
public class MemoryCacheProvider : ICacheProvider
{
private ObjectCache Cache => MemoryCache.Default;
private readonly object _lock = new object();
/// <summary>
/// Gets the item from the cache, if the item is not present
@ -91,9 +92,11 @@ namespace PlexRequests.Helpers
/// <returns></returns>
public T Get<T>(string key) where T : class
{
lock (key)
lock (_lock)
{
return Cache.Get(key) as T;
}
}
/// <summary>
/// Set/Store the specified object in the cache
@ -104,7 +107,7 @@ 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)
lock (_lock)
{
Cache.Remove(key);
Cache.Add(new CacheItem(key, data), policy);
@ -120,7 +123,7 @@ namespace PlexRequests.Helpers
var keys = Cache.Where(x => x.Key.Contains(key));
foreach (var k in keys)
{
lock (key)
lock (_lock)
{
Cache.Remove(k.Key);
}

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

@ -241,7 +241,7 @@ namespace PlexRequests.Services.Jobs
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)
{
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.
/// </summary>
/// <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)
{
Log.Info("Episode cache info is not available.");
return new List<PlexEpisodeModel>();
return new HashSet<PlexEpisodeModel>();
}
return episodes;
}

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

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

Loading…
Cancel
Save