Add caching for all tvdb requests

pull/776/head
Claus Vium 6 years ago
parent d6835f8dd6
commit 1f8e74f3a8

@ -9,6 +9,7 @@ using TvDbSharper.Dto;
namespace MediaBrowser.Providers.TV namespace MediaBrowser.Providers.TV
{ {
// TODO add to DI once Bond's PR is merged
public sealed class TvDbClientManager public sealed class TvDbClientManager
{ {
private static volatile TvDbClientManager instance; private static volatile TvDbClientManager instance;
@ -72,22 +73,45 @@ namespace MediaBrowser.Providers.TV
} }
} }
public async Task<SeriesSearchResult[]> GetSeriesByName(string name, CancellationToken cancellationToken) public Task<TvDbResponse<SeriesSearchResult[]>> GetSeriesByNameAsync(string name, CancellationToken cancellationToken)
{ {
return await TryGetValue(name, return TryGetValue(name,() => TvDbClient.Search.SearchSeriesByNameAsync(name, cancellationToken));
async () => (await TvDbClient.Search.SearchSeriesByNameAsync(name, cancellationToken)).Data);
} }
public async Task<Series> GetSeriesById(int tvdbId, CancellationToken cancellationToken) public Task<TvDbResponse<Series>> GetSeriesByIdAsync(int tvdbId, CancellationToken cancellationToken)
{ {
return await TryGetValue(tvdbId, return TryGetValue(tvdbId,() => TvDbClient.Series.GetAsync(tvdbId, cancellationToken));
async () => (await TvDbClient.Series.GetAsync(tvdbId, cancellationToken)).Data); }
public Task<TvDbResponse<EpisodeRecord>> GetEpisodesAsync(int tvdbId, CancellationToken cancellationToken)
{
return TryGetValue(tvdbId,() => TvDbClient.Episodes.GetAsync(tvdbId, cancellationToken));
}
public Task<TvDbResponse<SeriesSearchResult[]>> GetSeriesByImdbIdAsync(string imdbId, CancellationToken cancellationToken)
{
return TryGetValue(imdbId,() => TvDbClient.Search.SearchSeriesByImdbIdAsync(imdbId, cancellationToken));
}
public Task<TvDbResponse<SeriesSearchResult[]>> GetSeriesByZap2ItIdAsync(string zap2ItId, CancellationToken cancellationToken)
{
return TryGetValue(zap2ItId,() => TvDbClient.Search.SearchSeriesByImdbIdAsync(zap2ItId, cancellationToken));
}
public Task<TvDbResponse<Actor[]>> GetActorsAsync(int tvdbId, CancellationToken cancellationToken)
{
return TryGetValue(tvdbId,() => TvDbClient.Series.GetActorsAsync(tvdbId, cancellationToken));
}
public Task<TvDbResponse<Image[]>> GetImagesAsync(int tvdbId, ImagesQuery imageQuery, CancellationToken cancellationToken)
{
return TryGetValue(tvdbId,() => TvDbClient.Series.GetImagesAsync(tvdbId, imageQuery, cancellationToken));
} }
private async Task<T> TryGetValue<T>(object key, Func<Task<T>> resultFactory) private async Task<T> TryGetValue<T>(object key, Func<Task<T>> resultFactory)
{ {
if (_cache.TryGetValue(key, out T cachedValue)) if (_cache.TryGetValue(key, out T cachedValue))
{ {
Console.WriteLine("Cache hit!!!");
return cachedValue; return cachedValue;
} }

@ -52,7 +52,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
{ {
var tvdbId = episode.GetProviderId(MetadataProviders.Tvdb); var tvdbId = episode.GetProviderId(MetadataProviders.Tvdb);
// Process images // Process images
var episodeResult = await _tvDbClientManager.TvDbClient.Episodes.GetAsync(Convert.ToInt32(tvdbId), cancellationToken); var episodeResult = await _tvDbClientManager.GetEpisodesAsync(Convert.ToInt32(tvdbId), cancellationToken);
var image = GetImageInfo(episodeResult.Data); var image = GetImageInfo(episodeResult.Data);
if (image != null) if (image != null)

@ -46,7 +46,7 @@ namespace MediaBrowser.Providers.TV
if (TvdbSeriesProvider.IsValidSeries(searchInfo.SeriesProviderIds)) if (TvdbSeriesProvider.IsValidSeries(searchInfo.SeriesProviderIds))
{ {
var episodeResult = var episodeResult =
await _tvDbClientManager.TvDbClient.Episodes.GetAsync((int)searchInfo.IndexNumber, cancellationToken); await _tvDbClientManager.GetEpisodesAsync((int)searchInfo.IndexNumber, cancellationToken);
var metadataResult = MapEpisodeToResult(searchInfo, episodeResult.Data); var metadataResult = MapEpisodeToResult(searchInfo, episodeResult.Data);
if (metadataResult.HasMetadata) if (metadataResult.HasMetadata)
@ -82,7 +82,7 @@ namespace MediaBrowser.Providers.TV
if (TvdbSeriesProvider.IsValidSeries(searchInfo.SeriesProviderIds) && if (TvdbSeriesProvider.IsValidSeries(searchInfo.SeriesProviderIds) &&
(searchInfo.IndexNumber.HasValue || searchInfo.PremiereDate.HasValue)) (searchInfo.IndexNumber.HasValue || searchInfo.PremiereDate.HasValue))
{ {
var episodeResult = await _tvDbClientManager.TvDbClient.Episodes.GetAsync(Convert.ToInt32(searchInfo.GetProviderId(MetadataProviders.Tvdb)), var episodeResult = await _tvDbClientManager.GetEpisodesAsync(Convert.ToInt32(searchInfo.GetProviderId(MetadataProviders.Tvdb)),
cancellationToken); cancellationToken);
result = MapEpisodeToResult(searchInfo, episodeResult.Data); result = MapEpisodeToResult(searchInfo, episodeResult.Data);

@ -78,7 +78,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
}; };
try try
{ {
var imageResults = await _tvDbClientManager.TvDbClient.Series.GetImagesAsync(tvdbId, imageQuery, cancellationToken); var imageResults = await _tvDbClientManager.GetImagesAsync(tvdbId, imageQuery, cancellationToken);
remoteImages.AddRange(GetImages(imageResults.Data, language)); remoteImages.AddRange(GetImages(imageResults.Data, language));
} }
catch (TvDbServerException e) catch (TvDbServerException e)

@ -67,7 +67,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
KeyType = keyType KeyType = keyType
}; };
var imageResults = var imageResults =
await _tvDbClientManager.TvDbClient.Series.GetImagesAsync(Convert.ToInt32(item.GetProviderId(MetadataProviders.Tvdb)), imageQuery, cancellationToken); await _tvDbClientManager.GetImagesAsync(Convert.ToInt32(item.GetProviderId(MetadataProviders.Tvdb)), imageQuery, cancellationToken);
remoteImages.AddRange(GetImages(imageResults.Data, language)); remoteImages.AddRange(GetImages(imageResults.Data, language));
} }

@ -123,17 +123,15 @@ namespace MediaBrowser.Providers.TV.TheTVDB
tvdbId = await GetSeriesByRemoteId(zap2It, MetadataProviders.Zap2It.ToString(), metadataLanguage, cancellationToken); tvdbId = await GetSeriesByRemoteId(zap2It, MetadataProviders.Zap2It.ToString(), metadataLanguage, cancellationToken);
} }
// TODO call this function elsewhere? var seriesResult = await _tvDbClientManager.GetSeriesByIdAsync(Convert.ToInt32(tvdbId), cancellationToken);
var seriesResult = await _tvDbClientManager.GetSeriesById(Convert.ToInt32(tvdbId), cancellationToken);
// TODO error handling MapSeriesToResult(result, seriesResult.Data);
MapSeriesToResult(result, seriesResult);
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
result.ResetPeople(); result.ResetPeople();
var actorsResult = await _tvDbClientManager.TvDbClient.Series.GetActorsAsync(Convert.ToInt32(tvdbId), cancellationToken); var actorsResult = await _tvDbClientManager.GetActorsAsync(Convert.ToInt32(tvdbId), cancellationToken);
MapActorsToResult(result, actorsResult.Data); MapActorsToResult(result, actorsResult.Data);
} }
@ -144,11 +142,11 @@ namespace MediaBrowser.Providers.TV.TheTVDB
if (string.Equals(idType, MetadataProviders.Zap2It.ToString(), StringComparison.OrdinalIgnoreCase)) if (string.Equals(idType, MetadataProviders.Zap2It.ToString(), StringComparison.OrdinalIgnoreCase))
{ {
result = await _tvDbClientManager.TvDbClient.Search.SearchSeriesByZap2ItIdAsync(id, cancellationToken); result = await _tvDbClientManager.GetSeriesByZap2ItIdAsync(id, cancellationToken);
} }
else else
{ {
result = await _tvDbClientManager.TvDbClient.Search.SearchSeriesByImdbIdAsync(id, cancellationToken); result = await _tvDbClientManager.GetSeriesByImdbIdAsync(id, cancellationToken);
} }
return result.Data.First().Id.ToString(); return result.Data.First().Id.ToString();
@ -201,10 +199,10 @@ namespace MediaBrowser.Providers.TV.TheTVDB
_tvDbClientManager.TvDbClient.AcceptedLanguage = NormalizeLanguage(language); _tvDbClientManager.TvDbClient.AcceptedLanguage = NormalizeLanguage(language);
var comparableName = GetComparableName(name); var comparableName = GetComparableName(name);
var list = new List<Tuple<List<string>, RemoteSearchResult>>(); var list = new List<Tuple<List<string>, RemoteSearchResult>>();
SeriesSearchResult[] result; TvDbResponse<SeriesSearchResult[]> result;
try try
{ {
result = await _tvDbClientManager.GetSeriesByName(comparableName, cancellationToken); result = await _tvDbClientManager.GetSeriesByNameAsync(comparableName, cancellationToken);
} }
catch (TvDbServerException e) catch (TvDbServerException e)
{ {
@ -212,7 +210,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
return new List<RemoteSearchResult>(); return new List<RemoteSearchResult>();
} }
foreach (var seriesSearchResult in result) foreach (var seriesSearchResult in result.Data)
{ {
var tvdbTitles = new List<string> var tvdbTitles = new List<string>
{ {
@ -232,9 +230,9 @@ namespace MediaBrowser.Providers.TV.TheTVDB
try try
{ {
var seriesSesult = var seriesSesult =
await _tvDbClientManager.GetSeriesById(seriesSearchResult.Id, cancellationToken); await _tvDbClientManager.GetSeriesByIdAsync(seriesSearchResult.Id, cancellationToken);
remoteSearchResult.SetProviderId(MetadataProviders.Imdb, seriesSesult.ImdbId); remoteSearchResult.SetProviderId(MetadataProviders.Imdb, seriesSesult.Data.ImdbId);
remoteSearchResult.SetProviderId(MetadataProviders.Zap2It, seriesSesult.Zap2itId); remoteSearchResult.SetProviderId(MetadataProviders.Zap2It, seriesSesult.Data.Zap2itId);
} }
catch (TvDbServerException e) catch (TvDbServerException e)
{ {

Loading…
Cancel
Save