Adding high performance Windows Azure Cache Service to your ASP.NET Web API

Strath

September 13th, 2013

Adding high performance Windows Azure Cache Service to your ASP.NET Web API

Microsoft has recently announced the preview release of Windows Azure Cache Service – intended to allow you to easily deploy high performance, dedicated, distributed cache for your applications.

You can read more about the feature (and it does seem really awesome at first glance), in the thorough announcement post by Scott Guthrie.

Let’s look at how you can leverage this powerful service from ASP.NET Web API.

Strathweb.CacheOutput

You may or may not know that, but a while ago, as a side project sparked by one of the blog posts, I created a Web API caching library – allowing you to quite easily mimic the MVC’s OutputCache attribute behavior on Web API controllers.

The library has been available on Nuget and Github for a while now, and by default uses System.Runtime.Caching. I will not go into many details of that library now, as everything is explained in Github readme.

However, the main points is, it exposes a caching provider interface, IApiOutputCache, which you can use to plug in your own custom providers. As may have guessed by now, I have created a provider for the new Windows Azure Cache Service which you can install from Nuget and plug into Strathweb.CacheOutput.

Creating Windows Azure Cache Service

First of all, you need to create a cache service. You can do that by going to your Azure management portal and do the following:

New > Data Services > Cache

If the feature is unavailable to you (currently a preview) you may have to go to this page and activate it for your account.

Once created, the cache should appear in the left hand menu:

It might take a while to create it – the other day I had to wait roughly 15 minutes (not sure why).

There are a few interesting things when it comes to scaling and managing the cache, but Scott explained it all in his post – in our case we are just interested in the key so that we could connect – you can get it by selecting your cache and pressing the “Manage Keys” at the bottom of the page (you will need to copy the “Primary Access Key”), and in the cache endpoint URL which you can get from the Dashboard tab.

Strathweb.CacheOutput.Azure

Next step is to install the Strathweb.CacheOutput.Azure provider from Nuget. It also depends on the core Strathweb.CacheOutput so that will get pulled too and doesn’t need to be installed separately.

Now the only configuration you need to do is add Azure connectivity information. In your app.config / web.config (depending if you are self- or web-hosting) add the following:

and

Finally, in the HttpConfiguration tell Web API to use the Azure Caching provider:

That’s it! Dead easy.

All the rest is the same as you’d normally to with StrathwebCacheOutput. For example you can define the following sample controller:

In this case, the library will cache all the requests in the mighty Windows Azure Cache for 100 seconds. Each variation of a response is cached separately based on the action arguments and the content type (the library supports conneg, so XML/JSON representation is cached separately).

Finally, the use of AutoInvalidateCacheOutput means that the POST/PUT on the resource flushes all the cached items from a given resource (controller). Mind you, there are other invalidation options, this is just a quick example. You can read more about other features on Github.

Enjoy the power of Azure!

Be Sociable, Share!