Distributed caching stores reusable data in a shared repository that multiple web servers can access. A local in-memory cache belongs to one server: another server must create its own entry, possibly from data retrieved at a different time. A shared cache avoids those conflicting copies and can survive a web-server restart without using the servers’ local memory. section 8.4
For a board-game API, distributed caching is useful when repeated requests would otherwise query the database. Cache at the highest practical level: HTTP response caching suits frequently requested responses that stay the same, while server-side in-memory or distributed caching can reduce database work for requests with varying parameters. These techniques can be combined. section 8.1 section 8.6
Choose an external cache provider, then register its service in ASP.NET Core:
- SQL Server can use an existing database connection, but needs a dedicated cache table. Install Microsoft.Extensions.Caching.SqlServer, register AddDistributedSqlServerCache, and create the table with the dotnet-sql-cache tool. section 8.4.2
- Redis is a distributed key-value store. Set up a Redis instance, install its provider package, keep its connection string in User Secrets, and register AddStackExchangeRedisCache instead of the SQL Server provider. section 8.4.1 section 8.4.3
Listing 8.7 Program.cs file: Distributed SQL Server cache service
builder.Services.AddDistributedSqlServerCache(options => #1
{
options.ConnectionString =
builder.Configuration.GetConnectionString("DefaultConnection"); #2
options.SchemaName = "dbo"; #2
options.TableName = "AppCache"; #2
});
The SQL Server registration reuses the configured database connection and directs cache entries to the dedicated dbo.AppCache table. Create that table separately with dotnet sql-cache create before using it. section 8.4.2
Cache a board-game API query
Inject IDistributedCache into a controller. For a mechanics GET request, form a cache key from the request type and serialized input so different request parameters use different entries. Try to retrieve the cached Mechanic array; on a miss, run the ordered, paged database query and store its result for 30 seconds. The cache helper serializes values to bytes and deserializes them on retrieval. section 8.4.2
builder.Services.AddStackExchangeRedisCache(options => #1
{
options.Configuration =
builder.Configuration["Redis:ConnectionString"]; #2
});
To switch providers, register the Redis cache with its connection string in place of the SQL Server registration. The controller continues using IDistributedCache, so its caching code does not need to change. section 8.4.3
Check setup before relying on the cache: the SQL Server table must exist, or the Redis connection settings must point to an accessible instance. For SQL Server practice, change the configured table from AppCache to SQLCache and create the new table with the CLI; leave the old table available so the change can be reversed. section 8.4.2 section 8.5.5



