liveBook · Manning

SqlServerCacheOptions

As explained in Pro ASP.NET Core 7, Tenth Edition

SqlServerCacheOptions is an options class that specifies the SQL Server connection and table settings for an ASP.NET Core SQL Server cache.

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

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Defines five practical settings and demonstrates registration, but does not detail cache-table internals or option implementation.
Examples
Uses CacheDb, dbo.DataCache, a JSON connection string, and a /sum endpoint whose cached response survives restart.
What it assumes
Assumes familiarity with ASP.NET Core configuration and distributed caching, while demonstrating the required option assignments.
What you can do after
Operate/configure a SQL Server distributed cache through AddDistributedSqlServerCache and application configuration.
How it connects
Connects AddDistributedSqlServerCache, IConfiguration, IDistributedCache, SQL Server, and session middleware.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.