Jackett/src/Jackett.Common/Services/CacheService.cs

102 lines
3.5 KiB
C#
Raw Normal View History

2015-07-26 14:22:20 +00:00
using AutoMapper;
using Jackett.Indexers;
2015-07-26 14:22:20 +00:00
using Jackett.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Feature/netcore preparation (#2035) * Move to use package reference for restoring nuget packages. * Return a task result for this async method. * Update to a supported version of the .NET Framework. This also has the side effect of allowing us to automatically generate our binding redirects on build. * Set the solution to target VS2017 * Update test solution csproj file to support being built through MSBuild 15 * Move to use package reference for restoring nuget packages. * Return a task result for this async method. * Update to a supported version of the .NET Framework. This also has the side effect of allowing us to automatically generate our binding redirects on build. * Set the solution to target VS2017 * Update test solution csproj file to support being built through MSBuild 15 * DateTimeRoutines does not have Nuget packages that support .NET Standard (and therefore .NET Core). We will have to include them for now until we can get rid of this dependency. * Move the interfaces into their own files. This will be useful when we share them between the .NET Core and .NET Framework WebAPI * Stage services that need to point to the new interface namespace. * Update CurlSharp to fix memory leak issue and support better runtime compatibility with OSX and Linux * Start spliting some interfaces into their own files - this will help by allowing us to split them out in the future into a seperate project so the actual implementations can stay within their respective architectures when required
2017-10-29 10:19:09 +00:00
using Jackett.Services.Interfaces;
2015-07-26 14:22:20 +00:00
namespace Jackett.Services
{
public class CacheService : ICacheService
{
private readonly List<TrackerCache> cache = new List<TrackerCache>();
2015-08-07 21:40:45 +00:00
private readonly int MAX_RESULTS_PER_TRACKER = 1000;
private readonly TimeSpan AGE_LIMIT = new TimeSpan(7, 0, 0, 0);
2015-07-26 14:22:20 +00:00
2015-08-07 21:40:45 +00:00
public void CacheRssResults(IIndexer indexer, IEnumerable<ReleaseInfo> releases)
2015-07-26 14:22:20 +00:00
{
lock (cache)
{
var trackerCache = cache.Where(c => c.TrackerId == indexer.ID).FirstOrDefault();
2015-07-26 14:22:20 +00:00
if (trackerCache == null)
{
trackerCache = new TrackerCache();
trackerCache.TrackerId = indexer.ID;
trackerCache.TrackerName = indexer.DisplayName;
2015-07-26 14:22:20 +00:00
cache.Add(trackerCache);
}
foreach(var release in releases.OrderByDescending(i=>i.PublishDate))
{
var existingItem = trackerCache.Results.Where(i => i.Result.Guid == release.Guid).FirstOrDefault();
if (existingItem == null)
{
existingItem = new CachedResult();
existingItem.Created = DateTime.Now;
trackerCache.Results.Add(existingItem);
}
existingItem.Result = release;
}
// Prune cache
foreach(var tracker in cache)
{
tracker.Results = tracker.Results.OrderByDescending(i => i.Created).Take(MAX_RESULTS_PER_TRACKER).ToList();
}
2015-08-07 21:40:45 +00:00
}
}
public int GetNewItemCount(IIndexer indexer, IEnumerable<ReleaseInfo> releases)
{
lock (cache)
{
int newItemCount = 0;
var trackerCache = cache.Where(c => c.TrackerId == indexer.ID).FirstOrDefault();
if (trackerCache != null)
{
foreach (var release in releases)
{
if (trackerCache.Results.Where(i => i.Result.Guid == release.Guid).Count() == 0)
{
newItemCount++;
}
}
}
else {
newItemCount++;
}
return newItemCount;
2015-07-26 14:22:20 +00:00
}
}
public List<TrackerCacheResult> GetCachedResults()
2015-07-26 14:22:20 +00:00
{
lock (cache)
{
var results = new List<TrackerCacheResult>();
foreach(var tracker in cache)
{
foreach(var release in tracker.Results.OrderByDescending(i => i.Result.PublishDate).Take(300))
2015-07-26 14:22:20 +00:00
{
var item = Mapper.Map<TrackerCacheResult>(release.Result);
item.FirstSeen = release.Created;
item.Tracker = tracker.TrackerName;
item.TrackerId = tracker.TrackerId;
2015-07-26 14:22:20 +00:00
item.Peers = item.Peers - item.Seeders; // Use peers as leechers
results.Add(item);
}
}
2015-08-07 21:40:45 +00:00
return results.Take(3000).OrderByDescending(i=>i.PublishDate).ToList();
2015-07-26 14:22:20 +00:00
}
}
}
}