Skip to content
This repository has been archived by the owner on Apr 13, 2024. It is now read-only.

Commit

Permalink
Adds EpisodeCachePolicy
Browse files Browse the repository at this point in the history
This allow us to also cache badrequests
  • Loading branch information
Marcos Cordeiro committed Mar 2, 2024
1 parent f10af3c commit c10ff59
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
49 changes: 49 additions & 0 deletions Wasari.Tvdb.Api/Policies/EpisodeCachePolicy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Microsoft.AspNetCore.OutputCaching;

namespace Wasari.Tvdb.Api.Policies;

public class EpisodeCachePolicy : IOutputCachePolicy
{
public static readonly EpisodeCachePolicy Instance = new();

private EpisodeCachePolicy()
{
}

public ValueTask CacheRequestAsync(OutputCacheContext context, CancellationToken cancellation)
{
var attemptOutputCaching = AttemptOutputCaching();
context.EnableOutputCaching = true;
context.AllowCacheLookup = attemptOutputCaching;
context.AllowCacheStorage = attemptOutputCaching;
context.AllowLocking = true;

var value = context.HttpContext.Request.Query["query"].ToString().Trim();
context.CacheVaryByRules.VaryByValues.Add(new KeyValuePair<string, string>("query", value));

context.ResponseExpirationTimeSpan = TimeSpan.FromMinutes(5);
return ValueTask.CompletedTask;
}

ValueTask IOutputCachePolicy.ServeFromCacheAsync
(OutputCacheContext context, CancellationToken cancellationToken)
{
return ValueTask.CompletedTask;
}

ValueTask IOutputCachePolicy.ServeResponseAsync
(OutputCacheContext context, CancellationToken cancellationToken)
{
var response = context.HttpContext.Response;

if (response.StatusCode != StatusCodes.Status200OK && response.StatusCode != StatusCodes.Status400BadRequest)
{
context.AllowCacheStorage = false;
return ValueTask.CompletedTask;
}

return ValueTask.CompletedTask;
}

private static bool AttemptOutputCaching() => true;
}
14 changes: 6 additions & 8 deletions Wasari.Tvdb.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
using Wasari.Tvdb;
using Wasari.Tvdb.Api.Policies;
using Wasari.Tvdb.Api.Services;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddResponseCaching();
builder.Services.AddTvdbServices();
builder.Services.AddOutputCache();
builder.Services.AddOutputCache(options =>
{
options.AddPolicy(nameof(EpisodeCachePolicy), EpisodeCachePolicy.Instance);
});
builder.Services.AddScoped<TvdbEpisodesService>();

var app = builder.Build();
app.UseOutputCache();
app.MapGet("/episodes", (TvdbEpisodesService tvdbEpisodesService, string query) => tvdbEpisodesService.GetEpisodes(query))
.CacheOutput(o => o.Expire(TimeSpan.FromMinutes(5))
.SetVaryByQuery(Array.Empty<string>())
.VaryByValue(context =>
{
var value = context.Request.Query["query"].ToString().Trim();
return new KeyValuePair<string, string>("query", value);
}));
.CacheOutput(nameof(EpisodeCachePolicy));

app.Run();

0 comments on commit c10ff59

Please sign in to comment.