Programming C#

Distributed SQL Server Cache

Table CacheStore

Column NameData TypeAllow Nulls
IdVARCHAR(256)No
ValueVARBINARY(MAX)No
ExpiresAtTimeDATETIMEOFFSET(7)No
SlidingExpirationInSecondsBIGINTYes
AbsoluteExpirationDATETIMEOFFSET(7)Yes

NuGet Package

Microsoft.Extensions.Caching.SqlServer

Configuration

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

Cache Controller

public class CacheController : Controller{
    IDistributedCache cache;
    public HomeController(IDistributedCache cache){
        this.cache = cache;
    }
    public IActionResult Index(){
        string? welcome = cache.GetString("welcome");
        if (welcome is null){
            welcome = "Welcome Ladies and Gentlemen";
            cache.SetString("welcome", welcome);            
        }
        return View(model: welcome);
    }
}