mirror of
https://github.com/Sonarr/Sonarr.git
synced 2025-01-29 11:23:02 +02:00
Tuned down DB logging
Added cache to scene mapping.
This commit is contained in:
parent
2a12b051bc
commit
acf54203e5
@ -75,6 +75,24 @@ namespace NzbDrone.Common.Test.CacheTests
|
||||
|
||||
_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
|
||||
|
@ -5,8 +5,8 @@ namespace NzbDrone.Common.Cache
|
||||
{
|
||||
public interface ICacheManger
|
||||
{
|
||||
ICached<T> GetCache<T>(Type type);
|
||||
ICached<T> GetCache<T>(object host);
|
||||
ICached<T> GetCache<T>(Type host, string name);
|
||||
ICached<T> GetCache<T>(Type host);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
return (ICached<T>)_cache.Get(type.FullName, () => new Cached<T>());
|
||||
Ensure.That(() => host).IsNotNull();
|
||||
return GetCache<T>(host, host.FullName);
|
||||
}
|
||||
|
||||
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>());
|
||||
}
|
||||
}
|
||||
}
|
@ -25,6 +25,13 @@ namespace NzbDrone.Common.Cache
|
||||
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)
|
||||
{
|
||||
Ensure.That(() => key).IsNotNullOrWhiteSpace();
|
||||
|
@ -10,5 +10,6 @@ namespace NzbDrone.Common.Cache
|
||||
void Clear();
|
||||
void Remove(string key);
|
||||
T Get(string key);
|
||||
T Find(string key);
|
||||
}
|
||||
}
|
@ -32,7 +32,7 @@ namespace NzbDrone.Core.Configuration
|
||||
public ConfigFileProvider(IEnvironmentProvider environmentProvider, ICacheManger cacheManger)
|
||||
{
|
||||
_environmentProvider = environmentProvider;
|
||||
_cache = cacheManger.GetCache<string>(this);
|
||||
_cache = cacheManger.GetCache<string>(this.GetType());
|
||||
_configFile = _environmentProvider.GetConfigPath();
|
||||
}
|
||||
|
||||
|
@ -6,8 +6,6 @@ namespace NzbDrone.Core.DataAugmentation.Scene
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Cache;
|
||||
using NzbDrone.Common.Messaging;
|
||||
using NzbDrone.Core.Lifecycle;
|
||||
|
||||
@ -22,33 +23,43 @@ namespace NzbDrone.Core.DataAugmentation.Scene
|
||||
private readonly ISceneMappingRepository _repository;
|
||||
private readonly ISceneMappingProxy _sceneMappingProxy;
|
||||
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;
|
||||
_sceneMappingProxy = sceneMappingProxy;
|
||||
|
||||
_getSceneNameCache = cacheManger.GetCache<SceneMapping>(GetType(), "scene_name");
|
||||
_gettvdbIdCache = cacheManger.GetCache<SceneMapping>(GetType(), "tvdb_id");
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
var mapping = _repository.FindByCleanTitle(cleanName);
|
||||
lock (mutex)
|
||||
{
|
||||
var mapping = _gettvdbIdCache.Find(cleanName);
|
||||
|
||||
if (mapping == null)
|
||||
return null;
|
||||
if (mapping == null)
|
||||
return null;
|
||||
|
||||
return mapping.TvdbId;
|
||||
return mapping.TvdbId;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -65,13 +76,22 @@ namespace NzbDrone.Core.DataAugmentation.Scene
|
||||
try
|
||||
{
|
||||
var mappings = _sceneMappingProxy.Fetch();
|
||||
|
||||
|
||||
lock (mutex)
|
||||
{
|
||||
if (mappings.Any())
|
||||
{
|
||||
_repository.Purge();
|
||||
_repository.InsertMany(mappings);
|
||||
|
||||
_gettvdbIdCache.Clear();
|
||||
_getSceneNameCache.Clear();
|
||||
|
||||
foreach (var sceneMapping in mappings)
|
||||
{
|
||||
_getSceneNameCache.Set(sceneMapping.TvdbId.ToString(), sceneMapping);
|
||||
_gettvdbIdCache.Set(sceneMapping.CleanTitle, sceneMapping);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -20,7 +20,7 @@ namespace NzbDrone.Core.Instrumentation
|
||||
{
|
||||
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.LoggingRules.Add(Rule);
|
||||
|
Loading…
x
Reference in New Issue
Block a user