diff --git a/src/NzbDrone.Api/Series/SeriesModule.cs b/src/NzbDrone.Api/Series/SeriesModule.cs index 1b20364e0..0ceb589d7 100644 --- a/src/NzbDrone.Api/Series/SeriesModule.cs +++ b/src/NzbDrone.Api/Series/SeriesModule.cs @@ -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; diff --git a/src/NzbDrone.Core.Test/DataAugmentationFixture/Scene/SceneMappingServiceFixture.cs b/src/NzbDrone.Core.Test/DataAugmentationFixture/Scene/SceneMappingServiceFixture.cs index 8faa4b997..e4dbf0859 100644 --- a/src/NzbDrone.Core.Test/DataAugmentationFixture/Scene/SceneMappingServiceFixture.cs +++ b/src/NzbDrone.Core.Test/DataAugmentationFixture/Scene/SceneMappingServiceFixture.cs @@ -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() + .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() + .Setup(s => s.All()) + .Returns(Builder.CreateListOfSize(1).Build()); + + + Subject.HandleAsync(new ApplicationStartedEvent()); + + Mocker.GetMock() + .Verify(v => v.All(), Times.Once()); + + Subject.FindTvDbId("title"); + + Mocker.GetMock() + .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); } } } diff --git a/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/MapFixture.cs b/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/MapFixture.cs index 70860de84..6f1d84a4b 100644 --- a/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/MapFixture.cs +++ b/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/MapFixture.cs @@ -129,7 +129,7 @@ namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests public void should_use_tvdbid_matching_when_alias_is_found() { Mocker.GetMock() - .Setup(s => s.GetTvDbId(It.IsAny())) + .Setup(s => s.FindTvDbId(It.IsAny())) .Returns(_series.TvdbId); Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria); diff --git a/src/NzbDrone.Core/DataAugmentation/Scene/SceneMappingService.cs b/src/NzbDrone.Core/DataAugmentation/Scene/SceneMappingService.cs index fe112a3e0..8128ffe6a 100644 --- a/src/NzbDrone.Core/DataAugmentation/Scene/SceneMappingService.cs +++ b/src/NzbDrone.Core/DataAugmentation/Scene/SceneMappingService.cs @@ -15,8 +15,8 @@ namespace NzbDrone.Core.DataAugmentation.Scene public interface ISceneMappingService { List GetSceneNames(int tvdbId, IEnumerable seasonNumbers); - Nullable GetTvDbId(string title); - List FindByTvdbid(int tvdbId); + Nullable FindTvDbId(string title); + List FindByTvdbId(int tvdbId); Nullable GetSeasonNumber(string title); } @@ -28,8 +28,8 @@ namespace NzbDrone.Core.DataAugmentation.Scene private readonly ISceneMappingRepository _repository; private readonly IEnumerable _sceneMappingProviders; private readonly Logger _logger; - private readonly ICached _gettvdbIdCache; - private readonly ICached> _findbytvdbIdCache; + private readonly ICached _getTvdbIdCache; + private readonly ICached> _findByTvdbIdCache; public SceneMappingService(ISceneMappingRepository repository, ICacheManager cacheManager, @@ -39,14 +39,14 @@ namespace NzbDrone.Core.DataAugmentation.Scene _repository = repository; _sceneMappingProviders = sceneMappingProviders; - _gettvdbIdCache = cacheManager.GetCache(GetType(), "tvdb_id"); - _findbytvdbIdCache = cacheManager.GetCache>(GetType(), "find_tvdb_id"); + _getTvdbIdCache = cacheManager.GetCache(GetType(), "tvdb_id"); + _findByTvdbIdCache = cacheManager.GetCache>(GetType(), "find_tvdb_id"); _logger = logger; } public List GetSceneNames(int tvdbId, IEnumerable 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 GetTvDbId(string title) + public Nullable 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 FindByTvdbid(int tvdbId) + public List 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 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()); } } diff --git a/src/NzbDrone.Core/Parser/ParsingService.cs b/src/NzbDrone.Core/Parser/ParsingService.cs index d79bb47a7..bdce55c5f 100644 --- a/src/NzbDrone.Core/Parser/ParsingService.cs +++ b/src/NzbDrone.Core/Parser/ParsingService.cs @@ -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) { diff --git a/src/NzbDrone.Core/Tv/SeriesService.cs b/src/NzbDrone.Core/Tv/SeriesService.cs index 1b786f1b0..1d15cd5f4 100644 --- a/src/NzbDrone.Core/Tv/SeriesService.cs +++ b/src/NzbDrone.Core/Tv/SeriesService.cs @@ -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) {