Monday 19 October 2015

Caching the WEBApi REST calls using CacheCow - In memory

Caching is a very important aspect when we are designing  a RESTfull web APIs. The main advantage is we do not need to send the same resource over and over again if the content is not updated.

Traditional way of doing the caching is using ETags and maintain these ETags manually in both server and client end. ETag(Entity Tag) is the property which client validates against each request to a resource. Client sends the  request with the If-None-Match: "ETag#". If the ETag not updated , the server returns the 304 - Not Modified . ie : Client has the latest version of the resource requested.

Installing and configuring the Cachecow is simple. All we need to do it install the nuget package to the WebApi project.

Install-Package CacheCow.Server -Version 0.4.12

Add the following code to the WebApiConfig.cs file.
 
var cacheCowCacheHandler = new CacheCow.Server.CachingHandler();
config.MessageHandlers.Add(cacheCowCacheHandler);

This would create an in-memory cache for requested resources (Weak resources which consist the 'W/' prefix in the ETag ). This is Ok for a single server application where there are no any load balances or web farms. I will tell you how to configure persistence caching using CacheCow in my next post.

No comments:

Post a Comment