1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-24 08:42:19 +02:00

Cache BestForTags briefly for better performance when processing releases.

This commit is contained in:
Taloth Saldono 2017-10-25 23:09:35 +02:00
parent 393996fe88
commit c34eec160f

View File

@ -1,5 +1,7 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Extensions;
namespace NzbDrone.Core.Profiles.Delay
@ -18,20 +20,26 @@ public interface IDelayProfileService
public class DelayProfileService : IDelayProfileService
{
private readonly IDelayProfileRepository _repo;
private readonly ICached<DelayProfile> _bestForTagsCache;
public DelayProfileService(IDelayProfileRepository repo)
public DelayProfileService(IDelayProfileRepository repo, ICacheManager cacheManager)
{
_repo = repo;
_bestForTagsCache = cacheManager.GetCache<DelayProfile>(GetType(), "best");
}
public DelayProfile Add(DelayProfile profile)
{
return _repo.Insert(profile);
var result = _repo.Insert(profile);
_bestForTagsCache.Clear();
return result;
}
public DelayProfile Update(DelayProfile profile)
{
return _repo.Update(profile);
var result = _repo.Update(profile);
_bestForTagsCache.Clear();
return result;
}
public void Delete(int id)
@ -48,6 +56,7 @@ public void Delete(int id)
}
_repo.UpdateMany(all);
_bestForTagsCache.Clear();
}
public List<DelayProfile> All()
@ -67,7 +76,14 @@ public List<DelayProfile> AllForTags(HashSet<int> tagIds)
public DelayProfile BestForTags(HashSet<int> tagIds)
{
return _repo.All().Where(r => r.Tags.Intersect(tagIds).Any() || r.Tags.Empty())
var key = "-" + tagIds.Select(v => v.ToString()).Join(",");
return _bestForTagsCache.Get(key, () => FetchBestForTags(tagIds), TimeSpan.FromSeconds(30));
}
private DelayProfile FetchBestForTags(HashSet<int> tagIds)
{
return _repo.All()
.Where(r => r.Tags.Intersect(tagIds).Any() || r.Tags.Empty())
.OrderBy(d => d.Order).First();
}
}