Using distributed cache in ASP.NET core with SQL Server

.NET

First, create the cache table by running:


dotnet sql-cache create "Data Source=MyServer;Initial Catalog=MyDb;Integrated Security=True;" dbo MyTable

Install with nuget: Microsoft.Extensions.Caching.SqlServer

Register the service in the DI


builder.Services.AddDistributedSqlServerCache(options =>
{
    options.ConnectionString = builder.Configuration.GetConnectionString(
        "DefaultConnection");
    options.SchemaName = "dbo";
    options.TableName = "MyTable";
});

Inject the IDistributedCache instance

Post a Comment

Previous Post Next Post