Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle watchlist cache evictions #19

Merged
merged 3 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/API/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ static async Task Main(string[] args)

builder.Services.AddCaching(opts => opts
.UseHybrid("metadata", opts => opts.SQLite.DatabasePath = "metadata.sqlite")
.UseInMemory("watchlist"));
.UseInMemory("watchlist")
.UseInMemory("plex-graphql"));

builder.Services.AddFetcharr();
builder.Services.AddControllers();
Expand Down
2 changes: 2 additions & 0 deletions src/Cache/InMemory/src/InMemoryCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public override async Task SetAsync<T>(string key, T value, TimeSpan? expiration
{
this._database[key] = new InMemoryCacheItem(value, _expiration);

Interlocked.Increment(ref this.CacheSize);

if(options.Value.SizeLimit > 0 && Interlocked.Read(ref this.CacheSize) >= options.Value.SizeLimit)
{
int itemsToRemove = (int) (Interlocked.Read(ref this.CacheSize) - options.Value.SizeLimit);
Expand Down
18 changes: 14 additions & 4 deletions src/Provider.Plex/src/PlexWatchlistClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Flurl.Http;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Fetcharr.Provider.Plex
Expand All @@ -16,12 +17,14 @@ namespace Fetcharr.Provider.Plex
/// </summary>
public class PlexWatchlistClient(
IOptions<FetcharrConfiguration> configuration,
[FromKeyedServices("watchlist")] ICachingProvider cachingProvider)
[FromKeyedServices("watchlist")] ICachingProvider cachingProvider,
ILogger<PlexWatchlistClient> logger)
{
private readonly FlurlClient _client =
new FlurlClient("https://metadata.provider.plex.tv/library/sections/watchlist/")
.WithHeader("X-Plex-Token", configuration.Value.Plex.ApiToken)
.WithHeader("X-Plex-Client-Identifier", "fetcharr");
.WithHeader("X-Plex-Client-Identifier", "fetcharr")
.AllowHttpStatus((int) HttpStatusCode.NotModified);

/// <summary>
/// If not <see langword="null" />, contains the E-Tag value of the last watchlist request.
Expand All @@ -48,10 +51,17 @@ public async Task<IEnumerable<WatchlistMetadataItem>> FetchWatchlistAsync(int of
CacheValue<IEnumerable<WatchlistMetadataItem>> cacheValue =
await cachingProvider.GetAsync<IEnumerable<WatchlistMetadataItem>>("watchlist");

if(cacheValue.HasValue)
// If Plex returned NotModified, but the cache is empty, it must've been evicted
// which means we have to resend the request without E-Tag caching.
if(!cacheValue.HasValue)
{
return cacheValue.Value;
logger.LogInformation("Watchlist cache has been evicted; re-sending request...");

this.lastEtag = null;
return await this.FetchWatchlistAsync(offset, limit);
}

return cacheValue.Value;
}

MediaResponse<WatchlistMetadataItem> watchlistContainer = await response
Expand Down