Server-side caching with IDistributedCache and Redis via .NET Aspire
Unlike previous recipes that focused on HTTP caching headers, IDistributedCache
provides direct control over server-side caching. When combined with Redis through .NET Aspire, we can cache specific data objects and share them across multiple server instances. This approach is particularly useful when you need fine-grained control over what gets cached and for how long.
Getting ready
Ensure that Docker Desktop is installed and running on your machine.
As we go through this recipe, make sure to install the Aspire packages for .NET 9, not for .NET 8.
Download the starter project here: https://github.com/PacktPublishing/ASP.NET-9-Web-API-Cookbook/tree/main/start/chapter10/DistributedCacheRedis.
How to do it...
- Set Up the .NET Aspire
AppHost
project:- Navigate to the
AppHost
project, which will configure the Redis resource. - Add the
Redis
hosting package:
dotnet add package Aspire.Hosting.Redis
- Navigate...
- Navigate to the