Implementing Caching in ASP.NET Core with SQL Server
July 05, 2021 by Anuraj
AspNetCore Caching SqlServer
This article will discuss about implementing caching in ASP.NET Core using SQL Server Distributed Cache. Caching can improve the performance and scalability of an app, especially when the app is hosted by a cloud service or a server farm. In this implementation, you will be implementing distributed caching using SQL Server. In asp.net core distributed caching can be implemented with the help of IDistributedCache
interface - you can start with MemoryCache in the development and in production you can switch to SQL Server or Redis or any other provider which implements IDistributedCache
interface.
First you need to install the dotnet tool which helps you to setup the caching infrastructure. You can do this by running the command dotnet tool install --global dotnet-sql-cache
. Once you install this tool, you can use this tool to create required table in SQL Server Database. You can do this using another command - dotnet sql-cache create "Data Source=.;Initial Catalog=BlogsDb;User Id=sa;Password=Demo@123" dbo BlogsCache
. The command is sql-cache
with create
argument. And it requires the parameters like Connection string, Database Scheme and table name. Once you execute the command it will create a table like the following.
If you don’t want to create a tool for creating a table - you can use the following SQL Script.
To use SQL Server Caching you need to install the package - Microsoft.Extensions.Caching.SqlServer
. This package contains the implementation of IDistributedCache
interface. You can install the nuget package by command - dotnet add package Microsoft.Extensions.Caching.SqlServer
. Now you have completed the configuration aspects. Next you need to open the Startup.cs
file, ConfigureServices
method and add the following code - this code will be used to perform dependency injection of IDistributedCache
interface to the controllers.
And now you’re able to access IDistributedCache
instance in controllers, like this.
Here is simple caching pattern which helps to cache a list of items.
Once you execute the code, the information stored in the table as binary. Since we didn’t configured any expiry - default expiry time - 20 minutes (1200 seconds) is applied.
You can configure expiration of items using DistributedCacheEntryOptions
class, you need to create an instance of this class and use it while you’re setting the cache. Here is the different expiration configuration you can set.
Property | Description |
---|---|
AbsoluteExpiration | Gets or sets an absolute expiration date for the cache entry. |
AbsoluteExpirationRelativeToNow | Gets or sets an absolute expiration time, relative to now. |
SlidingExpiration | Gets or sets how long a cache entry can be inactive (e.g. not accessed) before it will be removed. This will not extend the entry lifetime beyond the absolute expiration (if set). |
Here is the updated code with expiration - which will expire the cache after one day based on the cache set.
You can remove the cache using RemoveAsync()
method, you need to provide the key as the parameter.
If you’re using ASP.NET Core MVC or Razor pages - ASP.NET Core provides a Distributed Cache Tag Helper - provides the ability to dramatically improve the performance of your ASP.NET Core app by caching its content to a distributed cache source. This tag helper can be used with the key provided while you set the cache.
This way you can implement Distributed Caching in ASP.NET Core with SQL Server. The advantage of this implementation or in fact any implementation of distributed caching in ASP.NET Core is you will be able to switch to any other provider without much code change. As I mentioned earlier for development environment you can use InMemory cache implementation and for production SQL Server.
Here are few links for you which might help you to find more details about different caching implementations and other helpful classes in ASP.NET Core.
- Distributed caching in ASP.NET Core
- Distributed Cache Tag Helper in ASP.NET Core
- Response Caching Middleware in ASP.NET Core
Happy Programming :)
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub