From c10ff59f55f8583065b5b9466854e6167e4a8105 Mon Sep 17 00:00:00 2001 From: Marcos Cordeiro Date: Sat, 2 Mar 2024 17:19:03 -0300 Subject: [PATCH] Adds EpisodeCachePolicy This allow us to also cache badrequests --- .../Policies/EpisodeCachePolicy.cs | 49 +++++++++++++++++++ Wasari.Tvdb.Api/Program.cs | 14 +++--- 2 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 Wasari.Tvdb.Api/Policies/EpisodeCachePolicy.cs diff --git a/Wasari.Tvdb.Api/Policies/EpisodeCachePolicy.cs b/Wasari.Tvdb.Api/Policies/EpisodeCachePolicy.cs new file mode 100644 index 0000000..7f0f946 --- /dev/null +++ b/Wasari.Tvdb.Api/Policies/EpisodeCachePolicy.cs @@ -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("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; +} \ No newline at end of file diff --git a/Wasari.Tvdb.Api/Program.cs b/Wasari.Tvdb.Api/Program.cs index 7ce983e..2ca0d43 100644 --- a/Wasari.Tvdb.Api/Program.cs +++ b/Wasari.Tvdb.Api/Program.cs @@ -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(); 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()) - .VaryByValue(context => - { - var value = context.Request.Query["query"].ToString().Trim(); - return new KeyValuePair("query", value); - })); + .CacheOutput(nameof(EpisodeCachePolicy)); app.Run(); \ No newline at end of file