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.

C#

1

install-package Strathweb.CacheOutput.Azure

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:

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

C#

1

2

3

4

5

6

7

8

9

10

11

[AutoInvalidateCacheOutput]

publicclassTestController:ApiController

{

[CacheOutput(ClientTimeSpan=100,ServerTimeSpan=100)]

publicstringGet(intid)

{

returnid.ToString();

}

publicvoidPost(){}

}

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.