Refresh the scene mapping cache if it is empty during a lookup

pull/2/head
Mark McDowall 10 years ago
parent 53c2962d2a
commit 438686ca1a

@ -175,7 +175,7 @@ namespace NzbDrone.Api.Series
private void PopulateAlternateTitles(SeriesResource resource)
{
var mappings = _sceneMappingService.FindByTvdbid(resource.TvdbId);
var mappings = _sceneMappingService.FindByTvdbId(resource.TvdbId);
if (mappings == null) return;

@ -6,6 +6,7 @@ using FizzWare.NBuilder;
using Moq;
using NUnit.Framework;
using NzbDrone.Core.DataAugmentation.Scene;
using NzbDrone.Core.Lifecycle;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common;
using FluentAssertions;
@ -103,6 +104,36 @@ namespace NzbDrone.Core.Test.DataAugmentationFixture.Scene
_provider2.Verify(c => c.GetSceneMappings(), Times.Once());
}
[Test]
public void should_refresh_cache_if_cache_is_empty_when_looking_for_tvdb_id()
{
Subject.FindTvDbId("title");
Mocker.GetMock<ISceneMappingRepository>()
.Verify(v => v.All(), Times.Once());
}
[Test]
public void should_not_refresh_cache_if_cache_is_not_empty_when_looking_for_tvdb_id()
{
GivenProviders(new[] { _provider1 });
Mocker.GetMock<ISceneMappingRepository>()
.Setup(s => s.All())
.Returns(Builder<SceneMapping>.CreateListOfSize(1).Build());
Subject.HandleAsync(new ApplicationStartedEvent());
Mocker.GetMock<ISceneMappingRepository>()
.Verify(v => v.All(), Times.Once());
Subject.FindTvDbId("title");
Mocker.GetMock<ISceneMappingRepository>()
.Verify(v => v.All(), Times.Once());
}
private void AssertNoUpdate()
{
_provider1.Verify(c => c.GetSceneMappings(), Times.Once());
@ -119,7 +150,7 @@ namespace NzbDrone.Core.Test.DataAugmentationFixture.Scene
foreach (var sceneMapping in _fakeMappings)
{
Subject.GetSceneNames(sceneMapping.TvdbId, _fakeMappings.Select(m => m.SeasonNumber)).Should().Contain(sceneMapping.SearchTerm);
Subject.GetTvDbId(sceneMapping.ParseTerm).Should().Be(sceneMapping.TvdbId);
Subject.FindTvDbId(sceneMapping.ParseTerm).Should().Be(sceneMapping.TvdbId);
}
}
}

@ -129,7 +129,7 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests
public void should_use_tvdbid_matching_when_alias_is_found()
{
Mocker.GetMock<ISceneMappingService>()
.Setup(s => s.GetTvDbId(It.IsAny<String>()))
.Setup(s => s.FindTvDbId(It.IsAny<String>()))
.Returns(_series.TvdbId);
Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria);

@ -15,8 +15,8 @@ namespace NzbDrone.Core.DataAugmentation.Scene
public interface ISceneMappingService
{
List<String> GetSceneNames(int tvdbId, IEnumerable<Int32> seasonNumbers);
Nullable<int> GetTvDbId(string title);
List<SceneMapping> FindByTvdbid(int tvdbId);
Nullable<int> FindTvDbId(string title);
List<SceneMapping> FindByTvdbId(int tvdbId);
Nullable<Int32> GetSeasonNumber(string title);
}
@ -28,8 +28,8 @@ namespace NzbDrone.Core.DataAugmentation.Scene
private readonly ISceneMappingRepository _repository;
private readonly IEnumerable<ISceneMappingProvider> _sceneMappingProviders;
private readonly Logger _logger;
private readonly ICached<SceneMapping> _gettvdbIdCache;
private readonly ICached<List<SceneMapping>> _findbytvdbIdCache;
private readonly ICached<SceneMapping> _getTvdbIdCache;
private readonly ICached<List<SceneMapping>> _findByTvdbIdCache;
public SceneMappingService(ISceneMappingRepository repository,
ICacheManager cacheManager,
@ -39,14 +39,14 @@ namespace NzbDrone.Core.DataAugmentation.Scene
_repository = repository;
_sceneMappingProviders = sceneMappingProviders;
_gettvdbIdCache = cacheManager.GetCache<SceneMapping>(GetType(), "tvdb_id");
_findbytvdbIdCache = cacheManager.GetCache<List<SceneMapping>>(GetType(), "find_tvdb_id");
_getTvdbIdCache = cacheManager.GetCache<SceneMapping>(GetType(), "tvdb_id");
_findByTvdbIdCache = cacheManager.GetCache<List<SceneMapping>>(GetType(), "find_tvdb_id");
_logger = logger;
}
public List<String> GetSceneNames(int tvdbId, IEnumerable<Int32> seasonNumbers)
{
var names = _findbytvdbIdCache.Find(tvdbId.ToString());
var names = _findByTvdbIdCache.Find(tvdbId.ToString());
if (names == null)
{
@ -58,9 +58,9 @@ namespace NzbDrone.Core.DataAugmentation.Scene
.Select(m => m.SearchTerm).Distinct().ToList());
}
public Nullable<Int32> GetTvDbId(string title)
public Nullable<Int32> FindTvDbId(string title)
{
var mapping = _gettvdbIdCache.Find(title.CleanSeriesTitle());
var mapping = FindTvdbId(title);
if (mapping == null)
return null;
@ -68,9 +68,14 @@ namespace NzbDrone.Core.DataAugmentation.Scene
return mapping.TvdbId;
}
public List<SceneMapping> FindByTvdbid(int tvdbId)
public List<SceneMapping> FindByTvdbId(int tvdbId)
{
var mappings = _findbytvdbIdCache.Find(tvdbId.ToString());
if (_findByTvdbIdCache.Count == 0)
{
RefreshCache();
}
var mappings = _findByTvdbIdCache.Find(tvdbId.ToString());
if (mappings == null)
{
@ -82,7 +87,7 @@ namespace NzbDrone.Core.DataAugmentation.Scene
public Nullable<Int32> GetSeasonNumber(string title)
{
var mapping = _gettvdbIdCache.Find(title.CleanSeriesTitle());
var mapping = FindTvdbId(title);
if (mapping == null)
return null;
@ -126,21 +131,31 @@ namespace NzbDrone.Core.DataAugmentation.Scene
RefreshCache();
}
private SceneMapping FindTvdbId(string title)
{
if (_getTvdbIdCache.Count == 0)
{
RefreshCache();
}
return _getTvdbIdCache.Find(title.CleanSeriesTitle());
}
private void RefreshCache()
{
var mappings = _repository.All().ToList();
_gettvdbIdCache.Clear();
_findbytvdbIdCache.Clear();
_getTvdbIdCache.Clear();
_findByTvdbIdCache.Clear();
foreach (var sceneMapping in mappings)
{
_gettvdbIdCache.Set(sceneMapping.ParseTerm.CleanSeriesTitle(), sceneMapping);
_getTvdbIdCache.Set(sceneMapping.ParseTerm.CleanSeriesTitle(), sceneMapping);
}
foreach (var sceneMapping in mappings.GroupBy(x => x.TvdbId))
{
_findbytvdbIdCache.Set(sceneMapping.Key.ToString(), sceneMapping.ToList());
_findByTvdbIdCache.Set(sceneMapping.Key.ToString(), sceneMapping.ToList());
}
}

@ -279,7 +279,7 @@ namespace NzbDrone.Core.Parser
{
if (searchCriteria != null)
{
var tvdbId = _sceneMappingService.GetTvDbId(title);
var tvdbId = _sceneMappingService.FindTvDbId(title);
if (tvdbId.HasValue)
{
if (searchCriteria.Series.TvdbId == tvdbId)
@ -336,7 +336,7 @@ namespace NzbDrone.Core.Parser
private Series GetSeries(ParsedEpisodeInfo parsedEpisodeInfo, int tvRageId, SearchCriteriaBase searchCriteria)
{
var tvdbId = _sceneMappingService.GetTvDbId(parsedEpisodeInfo.SeriesTitle);
var tvdbId = _sceneMappingService.FindTvDbId(parsedEpisodeInfo.SeriesTitle);
if (tvdbId.HasValue)
{

@ -93,7 +93,7 @@ namespace NzbDrone.Core.Tv
public Series FindByTitle(string title)
{
var tvdbId = _sceneMappingService.GetTvDbId(title);
var tvdbId = _sceneMappingService.FindTvDbId(title);
if (tvdbId.HasValue)
{

Loading…
Cancel
Save