Skip to content

Commit

Permalink
add basic memory caching support
Browse files Browse the repository at this point in the history
  • Loading branch information
kookxiang committed Aug 28, 2024
1 parent ef476e8 commit 9c2e266
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
49 changes: 49 additions & 0 deletions Jellyfin.Plugin.Bangumi/BangumiApi.Cache.cs
Original file line number Diff line number Diff line change
@@ -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<string> SendRequest(HttpRequestMessage request, string? accessToken, CancellationToken token)
{
return SendRequestWithCache(request, accessToken, token);
}

private Task<string> SendRequestWithCache(HttpRequestMessage request, string? accessToken, CancellationToken token)
{
if (request.Method != HttpMethod.Get || request.RequestUri == null) return SendRequestWithOutCache(request, accessToken, token);

return _cache.GetOrCreateAsync<string>(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<string> 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);
}
}
13 changes: 2 additions & 11 deletions Jellyfin.Plugin.Bangumi/BangumiApi.Jellyfin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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, ILogger<BangumiApi> logger)
{
private static readonly JsonSerializerOptions Options = new()
{
Expand All @@ -29,16 +30,6 @@ private async Task<string> SendRequest(HttpRequestMessage request, CancellationT
return await SendRequest(request, store.GetAvailable()?.AccessToken, token);
}

private async Task<string> 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<T?> SendRequest<T>(string url, CancellationToken token)
{
return await SendRequest<T>(url, store.GetAvailable()?.AccessToken, token);
Expand Down

0 comments on commit 9c2e266

Please sign in to comment.