SqlServerCacheOptions configures the SQL Server backing store used by AddDistributedSqlServerCache. It points the cache at a connection string, schema, and table, allowing cached data to survive application restarts and be shared across servers or containers. section 17.2.2
The most useful settings are:
- ConnectionString selects the database, conventionally read from IConfiguration. section 17.2.2
- SchemaName and TableName identify the cache table, such as dbo.DataCache. section 17.2.2
- ExpiredItemsDeletionInterval controls expired-item scans; the default is 30 minutes. section 17.2.2
- DefaultSlidingExpiration controls unread item lifetime; the default is 20 minutes. section 17.2.2
Listing 17.13 Using a persistent data cache in the Program.cs file in the Platform folder
var builder = WebApplication.CreateBuilder(args);
//builder.Services.AddDistributedMemoryCache(opts => {
// opts.SizeLimit = 200;
//});
builder.Services.AddDistributedSqlServerCache(opts => {
opts.ConnectionString
= builder.Configuration["ConnectionStrings:CacheConnection"];
opts.SchemaName = "dbo";
opts.TableName = "DataCache";
});
var app = builder.Build();
app.MapEndpoint<Platform.SumEndpoint>("/sum/{count:int=1000000000}");
app.MapGet("/", async context => {
await context.Response.WriteAsync("Hello World!");
});
app.Run();
A complete Program.cs registration reads ConnectionStrings:CacheConnection and configures dbo.DataCache through AddDistributedSqlServerCache. section 17.2.2
