2015-07-26 14:22:20 +00:00
|
|
|
|
using AutoMapper;
|
2015-08-05 22:00:09 +00:00
|
|
|
|
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;
|
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;
|
2015-08-06 19:07:58 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2015-08-05 22:00:09 +00:00
|
|
|
|
var trackerCache = cache.Where(c => c.TrackerId == indexer.ID).FirstOrDefault();
|
2015-07-26 14:22:20 +00:00
|
|
|
|
if (trackerCache == null)
|
|
|
|
|
{
|
|
|
|
|
trackerCache = new TrackerCache();
|
2015-08-05 22:00:09 +00:00
|
|
|
|
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++;
|
|
|
|
|
}
|
2015-08-06 19:07:58 +00:00
|
|
|
|
|
|
|
|
|
return newItemCount;
|
2015-07-26 14:22:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-13 21:51:49 +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)
|
|
|
|
|
{
|
2015-08-13 21:51:49 +00:00
|
|
|
|
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;
|
2015-08-05 22:00:09 +00:00
|
|
|
|
item.Tracker = tracker.TrackerName;
|
2015-08-13 21:51:49 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|