Cache BestForTags briefly for better performance when processing releases

pull/131/head
Qstick 7 years ago
parent b3fbd81594
commit 4f6415b0e5

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Extensions;
namespace NzbDrone.Core.Profiles.Delay
@ -20,22 +22,29 @@ namespace NzbDrone.Core.Profiles.Delay
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)
{
profile.Order = _repo.Count();
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)
@ -52,6 +61,7 @@ namespace NzbDrone.Core.Profiles.Delay
}
_repo.UpdateMany(all);
_bestForTagsCache.Clear();
}
public List<DelayProfile> All()
@ -77,8 +87,15 @@ namespace NzbDrone.Core.Profiles.Delay
public DelayProfile BestForTags(HashSet<int> tagIds)
{
return _repo.All().Where(r => r.Tags.Intersect(tagIds).Any() || r.Tags.Empty())
.OrderBy(d => d.Order).First();
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();
}
public List<DelayProfile> Reorder(int id, int? afterId)

Loading…
Cancel
Save