Distributed Caching in ASP.NET Core
By: Sankhadip Samanta
Tags: Postman, Sql Server, Caching, Dot Net Core, Aspnetcore

Implementing project using Visual Studio

Creating new project

Search for ASP.NET core web application and select it. Then click on Next

Name the Project DistributedCaching and select all dropdown according to above selection. Then click on Create

Choose API on next screen

Package Installation

Right-click on Project Solution select Manage Nuget Package and Install the following packages

· System.Data.SqlClient

Configuration

After the project creation, go to Startup.cs. In the ConfigureServices method, we need to add Distributed Memory Cache service.

In appsettings.json, we need to write our SQL server data source, user, password, and database that will be used by our project

"ConnectionStrings": { "SqlServerCString": "Data Source=localhost;Initial Catalog=caching;Persist Security Info=True;User ID=sa;Password=1234;" }

A complete view of appsettings.json

Create Database

Open your SQL Server Management Studio and create a database named caching.

Then open your query window and create a table named Student with the following query and insert some data using the following query

Create Controller

Click on the Controllers folder and add a new Controller. On the next screen select API Controller Empty and Click on Add

On the next screen name the controller CachingController and click on Add

Then open CachingController and do the following

[Route("api/[controller]")]

to

[Route("api/caching")]

Create Database Model

Create a folder and name it Model, add Student.cs with the following code. Make Model serializable for further serializing in setting cache

Place the below code inside CachingController

After placing code, make a request from POSTMAN to https://localhost:<PORT>/api/caching and you will get back the following response. You can notice response time 1348 MS

Create a folder Service and add MemoryCache.cs class and IMemoryCache.cs with the following code.

IMemoryCache.cs

MemoryCache.cs

After placing all code accordingly, we will do Dependency Injection MemoryCache in Startup.cs. Add the following code inside the ConfigureServices method

Now MemoryCache implementation available throughout Application Lifetime and an instance will be created every time there will be a request to the server.

Now let’s go for caching the result after fetching data from Student Table

First, let’s receive MemoryCache through Constructor Injection in CachingController.cs with the following code.

· Let’s cache student list after fetching data from Student Table, update the GetResult method in CachingController with the following code

It will set cache named student and send back response to Client.

So now fetched data from DB is now stored in a cache, now we have to check if student data available in the cache then fetch data from the cache otherwise make DB call and store it in the cache using the same method. Update the GetResult method with the following code.

Check if it is in cache then extract and send back otherwise make DB hit and set cache, send back to client

So now make a request from POSTMAN to https://localhost:<PORT>/api/caching and see the difference in response time

Too less than before

You can see time 7 MS, this makes a huge difference in server efficiency using the caching method.

Now, we have created a service that will be injected in Startup.cs file, a component that can be used in any project for caching. One more point if you want to remove a particular cache from memory then in the same way call removeCache and pass the CacheKeys inside the function. It will remove cache from memory.