1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-09-17 15:02:34 +02:00

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

This commit is contained in:
Mark McDowall 2014-11-18 23:47:19 -08:00
parent 53c2962d2a
commit 438686ca1a
6 changed files with 68 additions and 22 deletions

View File

@ -175,7 +175,7 @@ private void PopulateAlternateTitles(List<SeriesResource> resources)
private void PopulateAlternateTitles(SeriesResource resource)
{
var mappings = _sceneMappingService.FindByTvdbid(resource.TvdbId);
var mappings = _sceneMappingService.FindByTvdbId(resource.TvdbId);
if (mappings == null) return;

View File

@ -6,6 +6,7 @@
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 @@ public void should_get_mappings_for_all_providers()
_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 @@ private void AssertMappingUpdated()
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);
}
}
}

View File

@ -129,7 +129,7 @@ public void should_FindByTvRageId_when_search_criteria_and_FIndByTitle_matching_
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);

View File

@ -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 @@ public class SceneMappingService : ISceneMappingService,
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 @@ public SceneMappingService(ISceneMappingRepository repository,
_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 @@ public List<String> GetSceneNames(int tvdbId, IEnumerable<Int32> seasonNumbers)
.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 @@ public Nullable<Int32> GetTvDbId(string title)
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 @@ public List<SceneMapping> FindByTvdbid(int tvdbId)
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 @@ private void UpdateMappings()
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());
}
}

View File

@ -279,7 +279,7 @@ public ParsedEpisodeInfo ParseSpecialEpisodeTitle(string title, int tvRageId, Se
{
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 @@ private ParsedEpisodeInfo ParseSpecialEpisodeTitle(string title, Series series)
private Series GetSeries(ParsedEpisodeInfo parsedEpisodeInfo, int tvRageId, SearchCriteriaBase searchCriteria)
{
var tvdbId = _sceneMappingService.GetTvDbId(parsedEpisodeInfo.SeriesTitle);
var tvdbId = _sceneMappingService.FindTvDbId(parsedEpisodeInfo.SeriesTitle);
if (tvdbId.HasValue)
{

View File

@ -93,7 +93,7 @@ public Series FindByTvRageId(int tvRageId)
public Series FindByTitle(string title)
{
var tvdbId = _sceneMappingService.GetTvDbId(title);
var tvdbId = _sceneMappingService.FindTvDbId(title);
if (tvdbId.HasValue)
{