Add caching for all tvdb requests
This commit is contained in:
parent
d6835f8dd6
commit
1f8e74f3a8
|
@ -9,6 +9,7 @@ using TvDbSharper.Dto;
|
|||
|
||||
namespace MediaBrowser.Providers.TV
|
||||
{
|
||||
// TODO add to DI once Bond's PR is merged
|
||||
public sealed class TvDbClientManager
|
||||
{
|
||||
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,
|
||||
async () => (await TvDbClient.Search.SearchSeriesByNameAsync(name, cancellationToken)).Data);
|
||||
return TryGetValue(name,() => TvDbClient.Search.SearchSeriesByNameAsync(name, cancellationToken));
|
||||
}
|
||||
|
||||
public async Task<Series> GetSeriesById(int tvdbId, CancellationToken cancellationToken)
|
||||
public Task<TvDbResponse<Series>> GetSeriesByIdAsync(int tvdbId, CancellationToken cancellationToken)
|
||||
{
|
||||
return await TryGetValue(tvdbId,
|
||||
async () => (await TvDbClient.Series.GetAsync(tvdbId, cancellationToken)).Data);
|
||||
return TryGetValue(tvdbId,() => TvDbClient.Series.GetAsync(tvdbId, cancellationToken));
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (_cache.TryGetValue(key, out T cachedValue))
|
||||
{
|
||||
Console.WriteLine("Cache hit!!!");
|
||||
return cachedValue;
|
||||
}
|
||||
|
||||
|
|
|
@ -52,7 +52,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
|
|||
{
|
||||
var tvdbId = episode.GetProviderId(MetadataProviders.Tvdb);
|
||||
// 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);
|
||||
if (image != null)
|
||||
|
|
|
@ -46,7 +46,7 @@ namespace MediaBrowser.Providers.TV
|
|||
if (TvdbSeriesProvider.IsValidSeries(searchInfo.SeriesProviderIds))
|
||||
{
|
||||
var episodeResult =
|
||||
await _tvDbClientManager.TvDbClient.Episodes.GetAsync((int)searchInfo.IndexNumber, cancellationToken);
|
||||
await _tvDbClientManager.GetEpisodesAsync((int)searchInfo.IndexNumber, cancellationToken);
|
||||
var metadataResult = MapEpisodeToResult(searchInfo, episodeResult.Data);
|
||||
|
||||
if (metadataResult.HasMetadata)
|
||||
|
@ -82,7 +82,7 @@ namespace MediaBrowser.Providers.TV
|
|||
if (TvdbSeriesProvider.IsValidSeries(searchInfo.SeriesProviderIds) &&
|
||||
(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);
|
||||
|
||||
result = MapEpisodeToResult(searchInfo, episodeResult.Data);
|
||||
|
|
|
@ -78,7 +78,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
|
|||
};
|
||||
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));
|
||||
}
|
||||
catch (TvDbServerException e)
|
||||
|
|
|
@ -67,7 +67,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
|
|||
KeyType = keyType
|
||||
};
|
||||
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));
|
||||
}
|
||||
|
|
|
@ -123,17 +123,15 @@ namespace MediaBrowser.Providers.TV.TheTVDB
|
|||
tvdbId = await GetSeriesByRemoteId(zap2It, MetadataProviders.Zap2It.ToString(), metadataLanguage, cancellationToken);
|
||||
}
|
||||
|
||||
// TODO call this function elsewhere?
|
||||
var seriesResult = await _tvDbClientManager.GetSeriesById(Convert.ToInt32(tvdbId), cancellationToken);
|
||||
var seriesResult = await _tvDbClientManager.GetSeriesByIdAsync(Convert.ToInt32(tvdbId), cancellationToken);
|
||||
|
||||
// TODO error handling
|
||||
MapSeriesToResult(result, seriesResult);
|
||||
MapSeriesToResult(result, seriesResult.Data);
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -144,11 +142,11 @@ namespace MediaBrowser.Providers.TV.TheTVDB
|
|||
|
||||
if (string.Equals(idType, MetadataProviders.Zap2It.ToString(), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
result = await _tvDbClientManager.TvDbClient.Search.SearchSeriesByZap2ItIdAsync(id, cancellationToken);
|
||||
result = await _tvDbClientManager.GetSeriesByZap2ItIdAsync(id, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = await _tvDbClientManager.TvDbClient.Search.SearchSeriesByImdbIdAsync(id, cancellationToken);
|
||||
result = await _tvDbClientManager.GetSeriesByImdbIdAsync(id, cancellationToken);
|
||||
}
|
||||
|
||||
return result.Data.First().Id.ToString();
|
||||
|
@ -201,10 +199,10 @@ namespace MediaBrowser.Providers.TV.TheTVDB
|
|||
_tvDbClientManager.TvDbClient.AcceptedLanguage = NormalizeLanguage(language);
|
||||
var comparableName = GetComparableName(name);
|
||||
var list = new List<Tuple<List<string>, RemoteSearchResult>>();
|
||||
SeriesSearchResult[] result;
|
||||
TvDbResponse<SeriesSearchResult[]> result;
|
||||
try
|
||||
{
|
||||
result = await _tvDbClientManager.GetSeriesByName(comparableName, cancellationToken);
|
||||
result = await _tvDbClientManager.GetSeriesByNameAsync(comparableName, cancellationToken);
|
||||
}
|
||||
catch (TvDbServerException e)
|
||||
{
|
||||
|
@ -212,7 +210,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
|
|||
return new List<RemoteSearchResult>();
|
||||
}
|
||||
|
||||
foreach (var seriesSearchResult in result)
|
||||
foreach (var seriesSearchResult in result.Data)
|
||||
{
|
||||
var tvdbTitles = new List<string>
|
||||
{
|
||||
|
@ -232,9 +230,9 @@ namespace MediaBrowser.Providers.TV.TheTVDB
|
|||
try
|
||||
{
|
||||
var seriesSesult =
|
||||
await _tvDbClientManager.GetSeriesById(seriesSearchResult.Id, cancellationToken);
|
||||
remoteSearchResult.SetProviderId(MetadataProviders.Imdb, seriesSesult.ImdbId);
|
||||
remoteSearchResult.SetProviderId(MetadataProviders.Zap2It, seriesSesult.Zap2itId);
|
||||
await _tvDbClientManager.GetSeriesByIdAsync(seriesSearchResult.Id, cancellationToken);
|
||||
remoteSearchResult.SetProviderId(MetadataProviders.Imdb, seriesSesult.Data.ImdbId);
|
||||
remoteSearchResult.SetProviderId(MetadataProviders.Zap2It, seriesSesult.Data.Zap2itId);
|
||||
}
|
||||
catch (TvDbServerException e)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue
Block a user