Add caching PoC

This commit is contained in:
Claus Vium 2019-02-07 20:28:19 +01:00
parent ecbc0538f6
commit 75d90c8e4c
3 changed files with 41 additions and 8 deletions

View file

@ -11,6 +11,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.2.0" />
<PackageReference Include="PlaylistsNET" Version="1.0.2" /> <PackageReference Include="PlaylistsNET" Version="1.0.2" />
<PackageReference Include="TvDbSharper" Version="2.0.0" /> <PackageReference Include="TvDbSharper" Version="2.0.0" />
</ItemGroup> </ItemGroup>

View file

@ -1,12 +1,19 @@
using System; using System;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Library;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Internal;
using TvDbSharper; using TvDbSharper;
using TvDbSharper.Dto;
namespace MediaBrowser.Providers.TV namespace MediaBrowser.Providers.TV
{ {
public sealed class TvDbClientManager public sealed class TvDbClientManager
{ {
private static volatile TvDbClientManager instance; private static volatile TvDbClientManager instance;
// TODO add to DI once Bond's PR is merged
private static MemoryCache _cache;
private static readonly object syncRoot = new object(); private static readonly object syncRoot = new object();
private static TvDbClient tvDbClient; private static TvDbClient tvDbClient;
private static DateTime tokenCreatedAt; private static DateTime tokenCreatedAt;
@ -30,7 +37,10 @@ namespace MediaBrowser.Providers.TV
lock (syncRoot) lock (syncRoot)
{ {
if (instance == null) if (instance == null)
{
instance = new TvDbClientManager(); instance = new TvDbClientManager();
_cache = new MemoryCache(new MemoryCacheOptions());
}
} }
return instance; return instance;
@ -60,5 +70,27 @@ namespace MediaBrowser.Providers.TV
return tvDbClient; return tvDbClient;
} }
} }
public async Task<SeriesSearchResult[]> GetSeriesByName(string name, CancellationToken cancellationToken)
{
if (_cache.TryGetValue(name, out SeriesSearchResult[] series))
{
return series;
}
var result = await TvDbClient.Search.SearchSeriesByNameAsync(name, cancellationToken);
_cache.Set(name, result.Data, DateTimeOffset.UtcNow.AddHours(1));
return result.Data;
}
public async Task<Series> GetSeriesById(int tvdbId, CancellationToken cancellationToken)
{
if (_cache.TryGetValue(tvdbId, out Series series))
{
return series;
}
var result = await TvDbClient.Series.GetAsync(tvdbId, cancellationToken);
_cache.Set(tvdbId, result.Data, DateTimeOffset.UtcNow.AddHours(1));
return result.Data;
}
} }
} }

View file

@ -124,10 +124,10 @@ namespace MediaBrowser.Providers.TV.TheTVDB
} }
// TODO call this function elsewhere? // TODO call this function elsewhere?
var seriesResult = await _tvDbClientManager.TvDbClient.Series.GetAsync(Convert.ToInt32(tvdbId), cancellationToken); var seriesResult = await _tvDbClientManager.GetSeriesById(Convert.ToInt32(tvdbId), cancellationToken);
// TODO error handling // TODO error handling
MapSeriesToResult(result, seriesResult.Data); MapSeriesToResult(result, seriesResult);
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
@ -201,10 +201,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>>();
TvDbResponse<SeriesSearchResult[]> result; SeriesSearchResult[] result;
try try
{ {
result = await _tvDbClientManager.TvDbClient.Search.SearchSeriesByNameAsync(comparableName, cancellationToken); result = await _tvDbClientManager.GetSeriesByName(comparableName, cancellationToken);
} }
catch (TvDbServerException e) catch (TvDbServerException e)
{ {
@ -212,7 +212,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
return new List<RemoteSearchResult>(); return new List<RemoteSearchResult>();
} }
foreach (var seriesSearchResult in result.Data) foreach (var seriesSearchResult in result)
{ {
var tvdbTitles = new List<string> var tvdbTitles = new List<string>
{ {
@ -232,9 +232,9 @@ namespace MediaBrowser.Providers.TV.TheTVDB
try try
{ {
var seriesSesult = var seriesSesult =
await _tvDbClientManager.TvDbClient.Series.GetAsync(seriesSearchResult.Id, cancellationToken); await _tvDbClientManager.GetSeriesById(seriesSearchResult.Id, cancellationToken);
remoteSearchResult.SetProviderId(MetadataProviders.Imdb, seriesSesult.Data.ImdbId); remoteSearchResult.SetProviderId(MetadataProviders.Imdb, seriesSesult.ImdbId);
remoteSearchResult.SetProviderId(MetadataProviders.Zap2It, seriesSesult.Data.Zap2itId); remoteSearchResult.SetProviderId(MetadataProviders.Zap2It, seriesSesult.Zap2itId);
} }
catch (TvDbServerException e) catch (TvDbServerException e)
{ {