Apply Caching In Web API Using CacheCow

In this post, I’ll share about EntityTag caching in Web API. Caching always plays a vital role when we have very frequent requests for model to server, which stores loads of potential information and removes the need to hit server again and again, which helps to enhance Web API performance and reduce the load on server hosting the API. EntityTag is an HttpHeader used for cache conditional requests for resource. EntityTag is also pronounced as ETag.

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

ETag's 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 illustrations further down 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.

Let’s look in practical implementation. This is the simple structure of an application as given below in image:

solution

Install of CacheCow library: Kindly find the below screen shots to install CacheCow server from NuGet package manager.

NuGet

Click on install button as shown in above screenshot. As soon as you install the CacheCow you will see,

CacheCow.Common.dll and CacheCow.Server dll as depicted in screen below:

CacheCow.Common

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

execute

  1. //Way to register CacheCowHandler in WebApi.Config file  
  2. var objCacheCow = new CacheCow.Server.CachingHandler(config,"");  
  3. config.MessageHandlers.Add(objCacheCow);  
You can read more about how handler works in ASP.NET Web API from here: Global and Per-Route Handler in ASP.NET Web API

The above register CacheCow handler scrutinizes each request and response and verifies the ETag value whether it matches with earlier request or it’s a completely new request. 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 fiddler as shown below and press execute,

execute

There is a point of consideration as the response received from server in respect to the request sent by client as shown below in image contains Response Header HTTP 200 OK with the ETag value (a unique representation of each resource).

output

Step 2:

We will utilize this ETag value for further communication in order to identify that whether the requested resource exists in the same form or has been changed. If the client sends frequent requesst to server for the same resource than same ETag value will be returned for this request which means no one has updated the value for requested resource.

Note: Kindly send If-None-match parameter in header of request.

header

Output will be like this as shown in given below link:

output

Step 3:

So send the new request using the ETag value received in response of last request as shown in depicted image below. For this purpose, I’ve made slight change in collection that existed in code behind and it should return the updated response value. New ETag value ensures that the requested resource has been changed at server placed in in-memory,

parsed

The above request sends the following output as in the following image:

output

The above image simply states that there is no chanes at server side for this resource. This is how we can manage In-memory caching in Web API and utilize this feature.
 
feature

Caching plays a vital role where client sends very frequent requess for a resource. CacheCow is one of the good providers to maintain caching in solutions though there are lots of other variants that exist. It is simple in use, easy to install and works very effectively. The one downside 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 ASP.NET:

Up Next
    Ebook Download
    View all
    Learn
    View all