Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Redis cache in .NET 8 is not taking effect?

I am playing with .NET 8 new Aspire feature.

I am using Redis cache following this example from Microsoft on an application with Razor pages. and I use Orchestration to register the cache following this example.

Here is my code Program.cs at AppHost project

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var builder = DistributedApplication.CreateBuilder(args);

var cache = builder.AddRedisContainer("rediscache");
builder.AddProject<Projects.WeatherApp_Web>("frontend")
    .WithReference(cache)

In her is service registration in Program.cs of frontend project:

var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults();
builder.AddRedisOutputCache("rediscache");
etc...

And app usage:

var app = builder.Build();
app.UseOutputCache();
etc...

And of course, in my Razor page class, the duration is set to 5 seconds, only for testing.

[OutputCache(Duration = 5)]

The issue is that the content of the page is updating always and not showing the caching results. There is no error. What can I do to fix this?

>Solution :

I have recently working on something similar and faced the same issue.

To get the cache working, you need to call app.UseOutputCache(); after routing app.UseRouting(); which is somewhere in your Programs.cs in your frontend project.

So your code should be something like

var app = builder.Build();

etc...

app.UseRouting();
app.UseOutputCache();

etc...

And it will work.

I was able to find this information in Microsoft documentation.

In Razor Pages apps and apps with controllers, UseOutputCache must be
called after UseRouting.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading