Reworked the cacher, fixed the memory leak. No more logging within tight loops.

pull/470/head
tidusjar 8 years ago
parent 98662ab74e
commit 4bccb8fcf0

@ -224,7 +224,7 @@ namespace PlexRequests.Api
var request = new RestRequest
{
Method = Method.GET,
Resource = "/library/metadata/{ratingKey}/allLeaves"
Resource = "/library/metadata/{ratingKey}"
};
request.AddUrlSegment("ratingKey", ratingKey);

@ -244,13 +244,11 @@ namespace PlexRequests.Services.Jobs
var episodes = Cache.Get<List<PlexEpisodeModel>>(CacheKeys.PlexEpisodes);
if (episodes == null)
{
Log.Trace("Episode 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);
return false;
}
foreach (var result in episodes)
{
Log.Trace("Result:");
Log.Trace(result.DumpJson());
if (result.Episodes.ProviderId.Equals(theTvDbId) && result.Episodes.EpisodeNumber == episode && result.Episodes.SeasonNumber == season)
{
return true;

@ -59,11 +59,12 @@ namespace PlexRequests.Services.Jobs
private ICacheProvider Cache { get; }
private IJobRecord Job { get; }
private const int ResultCount = 25;
private const string PlexType = "episode";
public void CacheEpisodes()
{
var results = new List<PlexSearch>();
var results = new PlexSearch();
var settings = Plex.GetSettings();
var sections = PlexApi.GetLibrarySections(settings.PlexAuthToken, settings.FullUri);
var tvSection = sections.Directories.FirstOrDefault(x => x.type.Equals(PlexMediaType.Show.ToString(), StringComparison.CurrentCultureIgnoreCase));
@ -73,28 +74,27 @@ namespace PlexRequests.Services.Jobs
int totalSize;
var episodes = PlexApi.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, tvSectionId, currentPosition, ResultCount);
results.Add(episodes);
results = episodes;
int.TryParse(episodes.TotalSize, out totalSize);
currentPosition += ResultCount;
while (currentPosition < totalSize)
{
results.Add(PlexApi.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, tvSectionId, currentPosition, ResultCount));
results.Video.AddRange(PlexApi.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, tvSectionId, currentPosition, ResultCount).Video);
currentPosition += ResultCount;
}
var filteredList = results.Video.Where(x => x.Type.Equals(PlexType, StringComparison.InvariantCultureIgnoreCase));
var episodesModel = new List<PlexEpisodeModel>();
var metadataList = new List<PlexEpisodeMetadata>();
foreach (var r in results)
{
foreach (var video in r.Video)
foreach (var video in filteredList)
{
var ratingKey = video.RatingKey;
var metadata = PlexApi.GetEpisodeMetaData(settings.PlexAuthToken, settings.FullUri, ratingKey);
metadataList.Add(metadata);
}
}
foreach (var m in metadataList)
{
@ -111,7 +111,7 @@ namespace PlexRequests.Services.Jobs
}
if (results.Any())
if (results.Video.Any())
{
Cache.Set(CacheKeys.PlexEpisodes, episodesModel, CacheKeys.TimeFrameMinutes.SchedulerCaching);
}

@ -951,11 +951,13 @@ namespace PlexRequests.UI.Modules
{
var seriesId = (int)Request.Query.tvId;
var s = await SonarrService.GetSettingsAsync();
var sonarrEnabled = s.Enabled;
var allResults = await RequestService.GetAllAsync();
var seriesTask = Task.Run(
() =>
{
if (s.Enabled)
if (sonarrEnabled)
{
var allSeries = SonarrApi.GetSeries(s.ApiKey, s.FullUri);
var selectedSeries = allSeries.FirstOrDefault(x => x.tvdbId == seriesId) ?? new Series();
@ -972,9 +974,13 @@ namespace PlexRequests.UI.Modules
var show = await Task.Run(() => TvApi.ShowLookupByTheTvDbId(seriesId));
var seasons = await Task.Run(() => TvApi.EpisodeLookup(show.id));
var sonarrEpisodes = new List<SonarrEpisodes>();
if (sonarrEnabled)
{
var sonarrSeries = await seriesTask;
var sonarrEp = SonarrApi.GetEpisodes(sonarrSeries.id.ToString(), s.ApiKey, s.FullUri);
var sonarrEpisodes = sonarrEp?.ToList() ?? new List<SonarrEpisodes>();
sonarrEpisodes = sonarrEp?.ToList() ?? new List<SonarrEpisodes>();
}
foreach (var ep in seasons)
{

Loading…
Cancel
Save