Skip to content

Commit

Permalink
properly configure output caching
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanBout committed Dec 5, 2024
1 parent e76eb0f commit 305c65d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion SimpleCDN/Endpoints/CDN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public static IEndpointRouteBuilder RegisterCDNEndpoints(this IEndpointRouteBuil
{
try
{

if (loader.GetFile(route) is CDNFile file)
{
var typedAccept = ctx.Request.GetTypedHeaders().Accept;
Expand Down Expand Up @@ -57,6 +56,7 @@ public static IEndpointRouteBuilder RegisterCDNEndpoints(this IEndpointRouteBuil
}).CacheOutput(policy =>
{
// cache the response for 1 minute to reduce load on the server
// this is another layer of caching, on top of the CDNLoader cache.
policy.Cache()
.Expire(TimeSpan.FromMinutes(1))
.SetVaryByRouteValue("route")
Expand Down
10 changes: 10 additions & 0 deletions SimpleCDN/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ private static void Main(string[] args)
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddCommandLine(args);

builder.Services.AddOutputCache(options =>
{
var configuration = options.ApplicationServices.GetRequiredService<CDNConfiguration>();

// output cache is 1/10th of memory cache
// in bytes in kilobytes
options.MaximumBodySize = configuration.MaxMemoryCacheSize * 100;
});

builder.Services.MapConfiguration();

// for now, we use a simple size-limited in-memory cache.
Expand All @@ -35,6 +44,7 @@ private static void Main(string[] args)
var app = builder.Build();

app.RegisterCDNEndpoints();
app.UseOutputCache();

app.Run();
}
Expand Down

0 comments on commit 305c65d

Please sign in to comment.