Microsoft.Extensions.Caching.SqlServer is a .NET package that provides an IDistributedCache implementation backed by SQL Server. Configure it in Program.cs with an existing connection string, schema, and dedicated AppCache table. section 8.4.2
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
});
Register the provider with AddDistributedSqlServerCache, reuse DefaultConnection, and direct cache entries to dbo.AppCache. section 8.4.2
Complete the setup in these steps:
- Install the Microsoft.Extensions.Caching.SqlServer NuGet package. section 8.4.2
- Install the version-matched dotnet-sql-cache tool and run sql-cache create with the connection string, dbo schema, and AppCache table. section 8.4.2
- Inject IDistributedCache into an API controller, derive a request-based cache key, retrieve cached results, and cache database query results with an expiration. section 8.4.2
