Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone
Windows Azure Logo

Windows Azure Logo (http://www.brandsoftheworld.com/logo/windows-azure-0)

New caching feature added to Windows Azure development is Azure In-Role cache where you could use Azure VM memory as cache for all the instances. This is good solution to most Azure developments as it is easy to implement and cost effective. Not many of us needs dedicated cache servers or appfabric cache service. However the problem I came up with Azure In-Role caching is using it during the development of my web application as most of the time it is used as standalone web application running on localhost instead of running as azure web role on dev fabric. On the other hand I wanted to isolate the cache provider from my application logic so that I could plug different cache providers later. I know there are several other cache plugins out there. One of the best I used in past is Glav Cache Adapter. However I’m not sure and couldn’t check it lately to find out whether it supports Azure In-Role cache.

Anyway the purpose of this post is to show you how to use such simple cache adapter which can be configured to use either Azure In-Role cache in production environment and simple and faster in-memory cache during the development.

 Identifying Required Functionality of Cache Client Adapter

Best way to understand what functions to be exposed by our adapter is to check what methods available in one of the underlying cache clients that we are going to use. In this case we have two options. One is our own memory cache client and the other one is Microsoft DataCache client which is used to access Azure In-Role cache. By looking at it and considering our application requirements, I’ve come across following main functions.

  1. Add object to cache with a key and expiration time.
  2. Get object from the cache providing a key and delegate in case cache doesn’t have the object specified by the key or in case existing object is expired.
  3. Remove an object from cache by giving its key
  4. Clear all elements of the cache

So I came up with following interface and methods. I hope it is clear for you if you have little bit of experience in C# programming. Anyway once we’re implementing the concrete classes functionality of these methods will be clear for you.

Func<T> directFn is the method passed in, so that it can be executed inside our cache adapter class to get the required object and save it in the cache in case we don’t have it at the moment or available object is already expired.

Implementing Cache Wrapper for Microsoft DataCache Client

This is the easiest part as we only need to wrap DataCache methods in our own class.

As you can see everything is as simple as just calling the DataCache methods except the in the case of we don’t have it in the cache we invoke directFn() and get the return value and save it in the cache before passing it back to the caller.

Implementing Memory Cache

We don’t need to do complex things here as this is only be using during development or when running in single instance. So we will be using Dictionary to keep our objects against the cache keys. However the major implementation I need to show here is how to handle the timeout or expiration of the cache objects when we deal with in-memory cache.

So the problem here is we need to know whether the object we found in Dictionary is expired or not. We cannot do if we’re going to store the object caller pass to us as it is, we need to wrap it inside another object which has some additional properties such as when it was added to the dictionary and what’s it timeout period. If you look at the wrapper class CacheObject at line 82 you see these properties. So what we do here is when adding the object sent by caller we create new CacheObject and store the caller’s object in Value field together with the key in Key field, then we take the current UTC time and assign it to AddedTimeUtc so that we know when it was added when we access it next time. If we save Timeout value we know whether the object is expired just by examine the properties of the object itself. Rest of the logic of IMemoryCache client is not the complex. Every time we access the object stored in the dictionary we check its added time compared to current UTC time together with Timeout value and if it is expired we simply discard it and save the new object provided by the caller or one that is obtained from method delegate.

Implementing Simple CacheFactory

Now we can create simple CacheFactory which returns either one of the cache client instance by checking the configuration field from Web.config.

Calling GetAndAddIfNotFound Method

This is how you could call the GetAndAddIfNotFound method providing required parameters.

 

Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone