mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-07-24 14:40:39 +02:00
More MemoryCache PoC
This commit is contained in:
parent
75d90c8e4c
commit
86940e96d5
|
@ -13,6 +13,7 @@ namespace MediaBrowser.Providers.TV
|
||||||
{
|
{
|
||||||
private static volatile TvDbClientManager instance;
|
private static volatile TvDbClientManager instance;
|
||||||
// TODO add to DI once Bond's PR is merged
|
// TODO add to DI once Bond's PR is merged
|
||||||
|
private readonly SemaphoreSlim _cacheWriteLock = new SemaphoreSlim(1, 1);
|
||||||
private static MemoryCache _cache;
|
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;
|
||||||
|
@ -73,24 +74,32 @@ namespace MediaBrowser.Providers.TV
|
||||||
|
|
||||||
public async Task<SeriesSearchResult[]> GetSeriesByName(string name, CancellationToken cancellationToken)
|
public async Task<SeriesSearchResult[]> GetSeriesByName(string name, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (_cache.TryGetValue(name, out SeriesSearchResult[] series))
|
return await TryGetValue(name,
|
||||||
{
|
async () => (await TvDbClient.Search.SearchSeriesByNameAsync(name, cancellationToken)).Data);
|
||||||
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)
|
public async Task<Series> GetSeriesById(int tvdbId, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (_cache.TryGetValue(tvdbId, out Series series))
|
return await TryGetValue(tvdbId,
|
||||||
|
async () => (await TvDbClient.Series.GetAsync(tvdbId, cancellationToken)).Data);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<T> TryGetValue<T>(object key, Func<Task<T>> resultFactory)
|
||||||
|
{
|
||||||
|
if (_cache.TryGetValue(key, out T cachedValue))
|
||||||
{
|
{
|
||||||
return series;
|
return cachedValue;
|
||||||
|
}
|
||||||
|
using (_cacheWriteLock)
|
||||||
|
{
|
||||||
|
if (_cache.TryGetValue(key, out cachedValue))
|
||||||
|
{
|
||||||
|
return cachedValue;
|
||||||
|
}
|
||||||
|
var result = await resultFactory.Invoke();
|
||||||
|
_cache.Set(key, result, DateTimeOffset.UtcNow.AddHours(1));
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
var result = await TvDbClient.Series.GetAsync(tvdbId, cancellationToken);
|
|
||||||
_cache.Set(tvdbId, result.Data, DateTimeOffset.UtcNow.AddHours(1));
|
|
||||||
return result.Data;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue