Skip to content

Commit

Permalink
hotfix: handle watchlist cache evictions correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnatamo committed Sep 14, 2024
1 parent 231e8b8 commit bf1c024
Showing 1 changed file with 14 additions and 4 deletions.
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

0 comments on commit bf1c024

Please sign in to comment.