Implemented cache for QualityDefinitionService to reduce db calls.

pull/78/head
Taloth Saldono 11 years ago
parent 45b1067d44
commit 0187df38ec

@ -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)

@ -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());

Loading…
Cancel
Save