This repository has been archived by the owner on Apr 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allow us to also cache badrequests
- Loading branch information
Marcos Cordeiro
committed
Mar 2, 2024
1 parent
f10af3c
commit c10ff59
Showing
2 changed files
with
55 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |