Implemented cache for QualityDefinitionService to reduce db calls.

This commit is contained in:
Taloth Saldono 2014-05-03 00:26:55 +02:00
parent 45b1067d44
commit 0187df38ec
2 changed files with 19 additions and 4 deletions

View File

@ -100,6 +100,9 @@ namespace NzbDrone.Core.Jobs
public void Handle(CommandExecutedEvent message)
{
if (message.Command.GetType().Name == "BroadcastSignalRMessage")
return;
var scheduledTask = _scheduledTaskRepository.All().SingleOrDefault(c => c.TypeName == message.Command.GetType().FullName);
if (scheduledTask != null)

View File

@ -4,6 +4,7 @@ using NLog;
using NzbDrone.Core.Lifecycle;
using NzbDrone.Core.Messaging.Events;
using System;
using NzbDrone.Common.Cache;
namespace NzbDrone.Core.Qualities
{
@ -17,22 +18,31 @@ namespace NzbDrone.Core.Qualities
public class QualityDefinitionService : IQualityDefinitionService, IHandle<ApplicationStartedEvent>
{
private readonly IQualityDefinitionRepository _qualityDefinitionRepository;
private readonly ICached<Dictionary<Quality, QualityDefinition>> _cache;
private readonly Logger _logger;
public QualityDefinitionService(IQualityDefinitionRepository qualityDefinitionRepository, Logger logger)
public QualityDefinitionService(IQualityDefinitionRepository qualityDefinitionRepository, ICacheManager cacheManager, Logger logger)
{
_qualityDefinitionRepository = qualityDefinitionRepository;
_cache = cacheManager.GetCache<Dictionary<Quality, QualityDefinition>>(this.GetType());
_logger = logger;
}
private Dictionary<Quality, QualityDefinition> GetAll()
{
return _cache.Get("all", () => _qualityDefinitionRepository.All().ToDictionary(v => v.Quality), TimeSpan.FromSeconds(5.0));
}
public void Update(QualityDefinition qualityDefinition)
{
_qualityDefinitionRepository.Update(qualityDefinition);
_cache.Clear();
}
public List<QualityDefinition> All()
{
return _qualityDefinitionRepository.All().ToList();
return GetAll().Values.ToList();
}
public QualityDefinition Get(Quality quality)
@ -40,7 +50,7 @@ namespace NzbDrone.Core.Qualities
if (quality == Quality.Unknown)
return new QualityDefinition(Quality.Unknown);
return _qualityDefinitionRepository.GetByQualityId((int)quality);
return GetAll()[quality];
}
public void InsertMissingDefinitions(List<QualityDefinition> allDefinitions)
@ -89,10 +99,12 @@ namespace NzbDrone.Core.Qualities
_qualityDefinitionRepository.Update(existingDefinitions[i]);
}
}
_cache.Clear();
}
public void Handle(ApplicationStartedEvent message)
{
{
_logger.Debug("Setting up default quality config");
InsertMissingDefinitions(Quality.DefaultQualityDefinitions.ToList());