From 38f02d0b7cf87dec1df6d994bb356db8a733dd8a Mon Sep 17 00:00:00 2001 From: Mia Winter Date: Sat, 3 Feb 2024 20:20:17 +0100 Subject: [PATCH] Added caching proxy for favicone to reduce load and increase performance --- Wave/Controllers/ApiProxy.cs | 10 ++++++++-- Wave/Program.cs | 11 +++++++++++ Wave/Wave.csproj | 2 ++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Wave/Controllers/ApiProxy.cs b/Wave/Controllers/ApiProxy.cs index e562f0c..dad7e87 100644 --- a/Wave/Controllers/ApiProxy.cs +++ b/Wave/Controllers/ApiProxy.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.OutputCaching; namespace Wave.Controllers; @@ -9,7 +10,8 @@ public class ApiProxy(HttpClient client) : ControllerBase { [Route("favicon/{host}")] [Produces("image/x-icon")] - [ResponseCache(Duration = 60*60*24*30, Location = ResponseCacheLocation.Any)] + [OutputCache(Duration = 60*60*24*30)] + [ResponseCache(Duration = 60*60*24, Location = ResponseCacheLocation.Any)] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task GetFavicon(string host, [FromQuery] int size = 32) { @@ -26,6 +28,10 @@ public class ApiProxy(HttpClient client) : ControllerBase { private async Task DoProxy(string url) { - return await Client.SendAsync(new HttpRequestMessage(HttpMethod.Get, url)); + return await Client.SendAsync(new HttpRequestMessage(HttpMethod.Get, url) { + Headers = { + {"User-Agent", "Wave/1.0 favicon endpoint caching proxy"} + } + }); } } \ No newline at end of file diff --git a/Wave/Program.cs b/Wave/Program.cs index 233f6d3..0dc111b 100644 --- a/Wave/Program.cs +++ b/Wave/Program.cs @@ -30,6 +30,7 @@ builder.Services.AddControllers(options => { options.OutputFormatters.Add(new SyndicationFeedFormatter()); }); +builder.Services.AddOutputCache(); #region Data Protection & Redis @@ -41,6 +42,14 @@ EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC, ValidationAlgorithm = ValidationAlgorithm.HMACSHA256 }); + builder.Services.AddStackExchangeRedisCache(options => { + options.Configuration = redisUri; + options.InstanceName = "WaveDistributedCache"; + }); + builder.Services.AddStackExchangeRedisOutputCache(options => { + options.Configuration = redisUri; + options.InstanceName = "WaveOutputCache"; + }); } else { builder.Services.AddDataProtection() .UseCryptographicAlgorithms(new AuthenticatedEncryptorConfiguration() { @@ -138,6 +147,7 @@ app.UseExceptionHandler("/Error", createScopeForErrors: true); } + app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = new FileExtensionContentTypeProvider { Mappings = { @@ -153,6 +163,7 @@ app.MapAdditionalIdentityEndpoints(); app.MapControllers(); +app.UseOutputCache(); app.UseRequestLocalization(); diff --git a/Wave/Wave.csproj b/Wave/Wave.csproj index 024b1cd..cf1ca19 100644 --- a/Wave/Wave.csproj +++ b/Wave/Wave.csproj @@ -19,8 +19,10 @@ + +