Distributed SQL Server Cache
Table CacheStore
| Column Name | Data Type | Allow Nulls |
| Id | VARCHAR(256) | No |
| Value | VARBINARY(MAX) | No |
| ExpiresAtTime | DATETIMEOFFSET(7) | No |
| SlidingExpirationInSeconds | BIGINT | Yes |
| AbsoluteExpiration | DATETIMEOFFSET(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);
}
}