Added caching proxy for favicone to reduce load and increase performance

This commit is contained in:
Mia Rose Winter 2024-02-03 20:20:17 +01:00
parent e1412eef52
commit 38f02d0b7c
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
3 changed files with 21 additions and 2 deletions

View file

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OutputCaching;
namespace Wave.Controllers; namespace Wave.Controllers;
@ -9,7 +10,8 @@ public class ApiProxy(HttpClient client) : ControllerBase {
[Route("favicon/{host}")] [Route("favicon/{host}")]
[Produces("image/x-icon")] [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.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task GetFavicon(string host, [FromQuery] int size = 32) { public async Task GetFavicon(string host, [FromQuery] int size = 32) {
@ -26,6 +28,10 @@ public class ApiProxy(HttpClient client) : ControllerBase {
private async Task<HttpResponseMessage> DoProxy(string url) { private async Task<HttpResponseMessage> 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"}
}
});
} }
} }

View file

@ -30,6 +30,7 @@
builder.Services.AddControllers(options => { builder.Services.AddControllers(options => {
options.OutputFormatters.Add(new SyndicationFeedFormatter()); options.OutputFormatters.Add(new SyndicationFeedFormatter());
}); });
builder.Services.AddOutputCache();
#region Data Protection & Redis #region Data Protection & Redis
@ -41,6 +42,14 @@
EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC, EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC,
ValidationAlgorithm = ValidationAlgorithm.HMACSHA256 ValidationAlgorithm = ValidationAlgorithm.HMACSHA256
}); });
builder.Services.AddStackExchangeRedisCache(options => {
options.Configuration = redisUri;
options.InstanceName = "WaveDistributedCache";
});
builder.Services.AddStackExchangeRedisOutputCache(options => {
options.Configuration = redisUri;
options.InstanceName = "WaveOutputCache";
});
} else { } else {
builder.Services.AddDataProtection() builder.Services.AddDataProtection()
.UseCryptographicAlgorithms(new AuthenticatedEncryptorConfiguration() { .UseCryptographicAlgorithms(new AuthenticatedEncryptorConfiguration() {
@ -138,6 +147,7 @@
app.UseExceptionHandler("/Error", createScopeForErrors: true); app.UseExceptionHandler("/Error", createScopeForErrors: true);
} }
app.UseStaticFiles(new StaticFileOptions { app.UseStaticFiles(new StaticFileOptions {
ContentTypeProvider = new FileExtensionContentTypeProvider { ContentTypeProvider = new FileExtensionContentTypeProvider {
Mappings = { Mappings = {
@ -153,6 +163,7 @@
app.MapAdditionalIdentityEndpoints(); app.MapAdditionalIdentityEndpoints();
app.MapControllers(); app.MapControllers();
app.UseOutputCache();
app.UseRequestLocalization(); app.UseRequestLocalization();

View file

@ -19,8 +19,10 @@
<PackageReference Include="Microsoft.AspNetCore.DataProtection.StackExchangeRedis" Version="8.0.1" /> <PackageReference Include="Microsoft.AspNetCore.DataProtection.StackExchangeRedis" Version="8.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.0" /> <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" /> <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OutputCaching.StackExchangeRedis" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.1" /> <PackageReference Include="Microsoft.Extensions.Options" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" /> <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />