Explore Persistence Caching In Web API Using CacheCow.Server.EntityTagStore.SqlServer

In the previous post Apply Caching in Web API Using CacheCow, I shared about applying caching in-memory using CacheCow and its benefits. In the continuation of that post I’ll share thoughts about persistence caching and its implementation. Caching always plays a vital role when we have very frequent requests from model to server to fetch the data. Caching helps to reduce frequent hits tothe  server and brings potential information in a minimal duration. Caching also helps to enhance Web API performance and reduce the load on server where Web API is hosted. EntityTag is an HttpHeader used for caching conditional requests for resource. EntityTag is also pronounced as ETag.

Let’s take a simple example which uses ETag like “Client sends a request to Http server with Etag value that holds an updated value for cached resource, server identifies this with ETag value whether client has an already updated value for resource or it should revert with a latest copy back to client.

ETag working behavior and implementation

This section elaborates about ETag and its working behavior. Actually ETag is a string representation which is created by server against each request for a resource and also varies as value is updated for resource. For example,

Initially client sends a request http://localhost:57888/api2/employees/getdetails/5 to server and ask for employeeId 5, as per initial request it won’t be cached and server will return the fresh copy of the requested resource with some ETag value. We’ll see this in an illustration later in this article. Later client sends the same request to server with header If-None-Matchalong with ETag value which client has received in body response of earlier (e.g. TR6truy7) request If server finds equal ETag value for the requested resource than server responds Http not modified otherwise server will respond with new ETag value. Header “If-None-Match” works only with HttpGet and HttpDelete.

Installation of SqlServerEntityTagStore

HttpGet and HttpDelete works in similar manner. ThoughHttpPut works slightly in different nature. For this purpose we’ve to install “CacheCow.Server.EntityTagStore.SqlServer” from NuGet manager. Right click on solution and choose manage NuGet Manager.

Manager

Type CacheCow.Server.EntityTagStore.SqlServer in search, the following window will appears shown below. Click on install button.

install

Install of CacheCow library: Click on install button as show on above screen. As soon as you install the CacheCowlibrary you will findCacheCow.Server.EntityTagStore.SqlServer.dllin solution explorer under reference folder as depicted in screen below:

reference

Once the installation has done, we need to register CacheCowSQlServerEntitytagStore handler in WebApiConfig.cs file like shown below in depicted image.

installation

  1. //Configure HTTP Caching SqlServerEntityTagStore using Entity Tags  
  2.   
  3. varconnString = System.Configuration.ConfigurationManager.ConnectionStrings["CacheCowConnectionString"].ConnectionString;  
  4.   
  5. vareTagStore = new CacheCow.Server.EntityTagStore.SqlServer.SqlServerEntityTagStore(connString);  
  6.   
  7. varcacheCowCacheHandler = newCacheCow.Server.CachingHandler(config,eTagStore,"");  
  8.   
  9. cacheCowCacheHandler.AddLastModifiedHeader = false;  
  10.   
  11. config.MessageHandlers.Add(cacheCowCacheHandler);  

You can read more about how handler works in ASP.NET Web API from here: Global and Per-Route Handler in Asp.Net WebApi

Once we are done with installation and code segment placing, next step is to execute the following database script placed under {ProjectPath}\packages\CacheCow.Server.EntityTagStore.SqlServer.1.0.0\scripts.Once executed the database script there will be a table dbo.CacheState and 6 procedures as shown in depicted image below:

procedures

Each procedure works in a different manner. Now I’ll perform PUT HTTP PUT request to understand how persistence caching works does.

We will see this complete step by step process to understand this here. I’m using fiddler for this purpose.

Step 1: Send the Initial request using Postman as shown below and press send button:

send

Response from server as given below in screen shot:

server

As soon as you receive a response for the latest request sent by client, there will be an entry relevant to resource in CacheState table exist in database as shown below in screen shot, and which is self-explanatory.

explantory

Step 2: 

We will utilize this recently received ETag value for further communication in order to update the record using HTTP PUT verb.

Note
:

Kindly send If-Match parameter in header of request to update the resource. Kindly refer the image shown below: If- Match has the value "07aa73fe997d4c199f9b4774007778ac" which is sent by the server in last request.

CacheState

The status 200 means resource has updated .

Step 3: 

In these steps the main objective is to identify if client has the latest copy of resource or not, which client is willing to update. So send the new request using the ETag value (not latest one) received in some old response. In short If the ETag value doesn’t match with the ETag value persist in database than will revert precompiled condition issue in response .Which means that client doesn’t have the latest copy to update the resource. So please get the latest copy of resource(Using HTTP GET) before sending the update request. Kindly refer the image shown below:

request

If I use the latest Etag value than it works fine and update the value.

done

Caching plays a vital role where client sends very frequent requests for a resource. CacheCowis one of the good providers to maintain caching in solution though lots of other variants exist. It is simple in use, easy to install, and works very effectively. The one failing point which I realized is that you have to keep the updated value of specific ETag sent by server response for each request.

Read more articles on SQL Server:

Next Recommended Readings