diff --git a/Jellyfin.Plugin.Bangumi/BangumiApi.Cache.cs b/Jellyfin.Plugin.Bangumi/BangumiApi.Cache.cs new file mode 100644 index 0000000..0bcbbbf --- /dev/null +++ b/Jellyfin.Plugin.Bangumi/BangumiApi.Cache.cs @@ -0,0 +1,49 @@ +using System; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.Logging; + +namespace Jellyfin.Plugin.Bangumi; + +public partial class BangumiApi +{ + private readonly MemoryCache _cache = new(new MemoryCacheOptions + { + ExpirationScanFrequency = TimeSpan.FromMinutes(1), + + SizeLimit = 256 * 1024 * 1024 + }); + + private Task SendRequest(HttpRequestMessage request, string? accessToken, CancellationToken token) + { + return SendRequestWithCache(request, accessToken, token); + } + + private Task SendRequestWithCache(HttpRequestMessage request, string? accessToken, CancellationToken token) + { + if (request.Method != HttpMethod.Get || request.RequestUri == null) return SendRequestWithOutCache(request, accessToken, token); + + return _cache.GetOrCreateAsync(request.RequestUri.ToString(), async entry => + { + logger.LogInformation("request api without cache: {url}", request.RequestUri); + var response = await SendRequestWithOutCache(request, accessToken, CancellationToken.None); + entry.Size = response.Length; + entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(7); + entry.SlidingExpiration = TimeSpan.FromHours(6); + return response; + })!; + } + + private async Task SendRequestWithOutCache(HttpRequestMessage request, string? accessToken, CancellationToken token) + { + var httpClient = GetHttpClient(); + if (!string.IsNullOrEmpty(accessToken)) + request.Headers.Authorization = AuthenticationHeaderValue.Parse("Bearer " + accessToken); + using var response = await httpClient.SendAsync(request, token); + if (!response.IsSuccessStatusCode) await ServerException.ThrowFrom(response); + return await response.Content.ReadAsStringAsync(token); + } +} \ No newline at end of file diff --git a/Jellyfin.Plugin.Bangumi/BangumiApi.Jellyfin.cs b/Jellyfin.Plugin.Bangumi/BangumiApi.Jellyfin.cs index ba1cb86..8b36645 100644 --- a/Jellyfin.Plugin.Bangumi/BangumiApi.Jellyfin.cs +++ b/Jellyfin.Plugin.Bangumi/BangumiApi.Jellyfin.cs @@ -7,10 +7,11 @@ using System.Threading.Tasks; using Jellyfin.Plugin.Bangumi.OAuth; using MediaBrowser.Common.Net; +using Microsoft.Extensions.Logging; namespace Jellyfin.Plugin.Bangumi; -public partial class BangumiApi(IHttpClientFactory httpClientFactory, OAuthStore store) +public partial class BangumiApi(IHttpClientFactory httpClientFactory, OAuthStore store, Logger logger) { private static readonly JsonSerializerOptions Options = new() { @@ -29,16 +30,6 @@ private async Task SendRequest(HttpRequestMessage request, CancellationT return await SendRequest(request, store.GetAvailable()?.AccessToken, token); } - private async Task SendRequest(HttpRequestMessage request, string? accessToken, CancellationToken token) - { - var httpClient = GetHttpClient(); - if (!string.IsNullOrEmpty(accessToken)) - request.Headers.Authorization = AuthenticationHeaderValue.Parse("Bearer " + accessToken); - using var response = await httpClient.SendAsync(request, token); - if (!response.IsSuccessStatusCode) await ServerException.ThrowFrom(response); - return await response.Content.ReadAsStringAsync(token); - } - private async Task SendRequest(string url, CancellationToken token) { return await SendRequest(url, store.GetAvailable()?.AccessToken, token);