Tuned down DB logging

Added cache to scene mapping.
This commit is contained in:
kay.one 2013-05-29 21:55:23 -07:00
parent 2a12b051bc
commit acf54203e5
8 changed files with 68 additions and 31 deletions

View File

@ -75,6 +75,24 @@ namespace NzbDrone.Common.Test.CacheTests
_cachedString.Get("Key").Should().Be("New"); _cachedString.Get("Key").Should().Be("New");
} }
[Test]
public void should_store_null()
{
int hitCount = 0;
for (int i = 0; i < 10; i++)
{
_cachedString.Get("key", () =>
{
hitCount++;
return null;
});
}
hitCount.Should().Be(1);
}
} }
public class Worker public class Worker

View File

@ -5,8 +5,8 @@ namespace NzbDrone.Common.Cache
{ {
public interface ICacheManger public interface ICacheManger
{ {
ICached<T> GetCache<T>(Type type); ICached<T> GetCache<T>(Type host, string name);
ICached<T> GetCache<T>(object host); ICached<T> GetCache<T>(Type host);
} }
public class CacheManger : ICacheManger public class CacheManger : ICacheManger
@ -19,16 +19,18 @@ namespace NzbDrone.Common.Cache
} }
public ICached<T> GetCache<T>(Type type) public ICached<T> GetCache<T>(Type host)
{ {
Ensure.That(() => type).IsNotNull(); Ensure.That(() => host).IsNotNull();
return GetCache<T>(host, host.FullName);
return (ICached<T>)_cache.Get(type.FullName, () => new Cached<T>());
} }
public ICached<T> GetCache<T>(object host) public ICached<T> GetCache<T>(Type host, string name)
{ {
return GetCache<T>(host.GetType()); Ensure.That(() => host).IsNotNull();
Ensure.That(() => name).IsNotNullOrWhiteSpace();
return (ICached<T>)_cache.Get(host.FullName + "_" + name, () => new Cached<T>());
} }
} }
} }

View File

@ -25,6 +25,13 @@ namespace NzbDrone.Common.Cache
return Get(key, () => { throw new KeyNotFoundException(key); }); return Get(key, () => { throw new KeyNotFoundException(key); });
} }
public T Find(string key)
{
T value;
_store.TryGetValue(key, out value);
return value;
}
public T Get(string key, Func<T> function) public T Get(string key, Func<T> function)
{ {
Ensure.That(() => key).IsNotNullOrWhiteSpace(); Ensure.That(() => key).IsNotNullOrWhiteSpace();

View File

@ -10,5 +10,6 @@ namespace NzbDrone.Common.Cache
void Clear(); void Clear();
void Remove(string key); void Remove(string key);
T Get(string key); T Get(string key);
T Find(string key);
} }
} }

View File

@ -32,7 +32,7 @@ namespace NzbDrone.Core.Configuration
public ConfigFileProvider(IEnvironmentProvider environmentProvider, ICacheManger cacheManger) public ConfigFileProvider(IEnvironmentProvider environmentProvider, ICacheManger cacheManger)
{ {
_environmentProvider = environmentProvider; _environmentProvider = environmentProvider;
_cache = cacheManger.GetCache<string>(this); _cache = cacheManger.GetCache<string>(this.GetType());
_configFile = _environmentProvider.GetConfigPath(); _configFile = _environmentProvider.GetConfigPath();
} }

View File

@ -6,8 +6,6 @@ namespace NzbDrone.Core.DataAugmentation.Scene
{ {
public interface ISceneMappingRepository : IBasicRepository<SceneMapping> public interface ISceneMappingRepository : IBasicRepository<SceneMapping>
{ {
SceneMapping FindByTvdbId(int tvdbId);
SceneMapping FindByCleanTitle(string cleanTitle);
} }
@ -18,14 +16,5 @@ namespace NzbDrone.Core.DataAugmentation.Scene
{ {
} }
public SceneMapping FindByTvdbId(int tvdbId)
{
return Query.SingleOrDefault(c => c.TvdbId == tvdbId);
}
public SceneMapping FindByCleanTitle(string cleanTitle)
{
return Query.SingleOrDefault(c => c.CleanTitle == cleanTitle);
}
} }
} }

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Linq; using System.Linq;
using NLog; using NLog;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Messaging; using NzbDrone.Common.Messaging;
using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Lifecycle;
@ -22,33 +23,43 @@ namespace NzbDrone.Core.DataAugmentation.Scene
private readonly ISceneMappingRepository _repository; private readonly ISceneMappingRepository _repository;
private readonly ISceneMappingProxy _sceneMappingProxy; private readonly ISceneMappingProxy _sceneMappingProxy;
private readonly Logger _logger; private readonly Logger _logger;
private readonly ICached<SceneMapping> _getSceneNameCache;
private readonly ICached<SceneMapping> _gettvdbIdCache;
public SceneMappingService(ISceneMappingRepository repository, ISceneMappingProxy sceneMappingProxy, Logger logger) public SceneMappingService(ISceneMappingRepository repository, ISceneMappingProxy sceneMappingProxy, ICacheManger cacheManger, Logger logger)
{ {
_repository = repository; _repository = repository;
_sceneMappingProxy = sceneMappingProxy; _sceneMappingProxy = sceneMappingProxy;
_getSceneNameCache = cacheManger.GetCache<SceneMapping>(GetType(), "scene_name");
_gettvdbIdCache = cacheManger.GetCache<SceneMapping>(GetType(), "tvdb_id");
_logger = logger; _logger = logger;
} }
public string GetSceneName(int tvdbId, int seasonNumber = -1) public string GetSceneName(int tvdbId, int seasonNumber = -1)
{ {
var mapping = _repository.FindByTvdbId(tvdbId); lock (mutex)
{
var mapping = _getSceneNameCache.Find(tvdbId.ToString());
if (mapping == null) return null; if (mapping == null) return null;
return mapping.SceneName; return mapping.SceneName;
}
} }
public Nullable<Int32> GetTvDbId(string cleanName) public Nullable<Int32> GetTvDbId(string cleanName)
{ {
var mapping = _repository.FindByCleanTitle(cleanName); lock (mutex)
{
var mapping = _gettvdbIdCache.Find(cleanName);
if (mapping == null) if (mapping == null)
return null; return null;
return mapping.TvdbId; return mapping.TvdbId;
}
} }
@ -65,13 +76,22 @@ namespace NzbDrone.Core.DataAugmentation.Scene
try try
{ {
var mappings = _sceneMappingProxy.Fetch(); var mappings = _sceneMappingProxy.Fetch();
lock (mutex) lock (mutex)
{ {
if (mappings.Any()) if (mappings.Any())
{ {
_repository.Purge(); _repository.Purge();
_repository.InsertMany(mappings); _repository.InsertMany(mappings);
_gettvdbIdCache.Clear();
_getSceneNameCache.Clear();
foreach (var sceneMapping in mappings)
{
_getSceneNameCache.Set(sceneMapping.TvdbId.ToString(), sceneMapping);
_gettvdbIdCache.Set(sceneMapping.CleanTitle, sceneMapping);
}
} }
else else
{ {

View File

@ -20,7 +20,7 @@ namespace NzbDrone.Core.Instrumentation
{ {
Layout = new SimpleLayout("${callsite:className=false:fileName=false:includeSourcePath=false:methodName=true}"); Layout = new SimpleLayout("${callsite:className=false:fileName=false:includeSourcePath=false:methodName=true}");
Rule = new LoggingRule("*", LogLevel.Trace, this); Rule = new LoggingRule("*", LogLevel.Debug, this);
LogManager.Configuration.AddTarget("DbLogger", this); LogManager.Configuration.AddTarget("DbLogger", this);
LogManager.Configuration.LoggingRules.Add(Rule); LogManager.Configuration.LoggingRules.Add(Rule);