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) public void Handle(CommandExecutedEvent message)
{ {
if (message.Command.GetType().Name == "BroadcastSignalRMessage")
return;
var scheduledTask = _scheduledTaskRepository.All().SingleOrDefault(c => c.TypeName == message.Command.GetType().FullName); var scheduledTask = _scheduledTaskRepository.All().SingleOrDefault(c => c.TypeName == message.Command.GetType().FullName);
if (scheduledTask != null) if (scheduledTask != null)

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

Loading…
Cancel
Save