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

var builder = DistributedApplication.CreateBuilder(args);

var cache = builder.AddRedisContainer("rediscache");

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

var builder = WebApplication.CreateBuilder(args);

And app usage:

var app = builder.Build();

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();




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.

Leave a Reply