Azure AppFabric cache with EPiServer CMS

This blog post is about using Windows Azure AppFabric cache service together with EPiServer CMS. EPiServer CMS supports pluggable cache, replacing the standard EPiServer CMS cache component.

Windows Azure AppFabric Cache

EPiServer CMS implements its own cache manager. You can read more about it here. One of the many features of the base implementation is that the cache manager can manage – where the main purpose is to invalidate cached objects – in a load-balanced environment.

This works great in an environment where the server instances are known since EPiServer CMS uses remoting. In a dynamic environment like Windows Azure where servers are added – and removed – on the fly we need another approach.

Instead of keeping the cache local we’ll access it through a service. Enter the AppFabric cache service. The strength of the cache service is that we access the cache through a single point – regardless of our server setup.

image

Building your own EPiServer CMS cache component

In order to utilize the AppFabric cache service we need to replace the built-in EPiServer CMS cache component. Fortunately EPiServer provides an easy way for us to do this through the pluggable runtime cache.

By implemeting the interface IRuntimeCache from EPiServer.BaseLibrary we can create a pluggable cache component. If you’re familiar with the basics of caching you’ll have no trouble sorting the methods out. See code sample below.

The component also needs to be registered in web.config. See sample configuration below.

Getting started with the AppFabric Cache service

Start off by intstalling the AppFabric Cache locally for development purposes. Both Wade Wegner and Scott Hanselman have guides on how to do this.

In your Windows Azure project you’ll need to add a two references as seen in the image below.

image

Finally the using statement should be added to your class as well

Using the Cache service is very simple from here. See sample code below.

Implementation in EPiServer CMS

Now that we know the very basics of how the AppFabric Cache works we’ll have a look at the two main methods of our pluggable runtime cache component for EPiServer CMS: Add and Get.

Adding items to the cache

Sample code of the Add-method.

Anything that is to be cached by the AppFabric Cache service needs to be serialized since we’re using the HTTP protocol to communicate with the AppFabric Cache service. However, the internal class CacheItem that EPiServer CMS utilizes to manage cached data is declared as private and cannot be serialized.

As per the code above we retrieve the serialized data in the CacheItem-type through reflection and create a new instance of our own SerializableCacheItem-class which is a simple class declared as public which contains the serialized data. See code below.

Just as we constructed an object we could serialize and transfer over the wire we need to reverse the process. In the same manner we utilize reflection to create an instance of the CacheItem-type and return it.

Conclusion

Should you use Windows Azure AppFabric Cache Service with your EPiServer CMS implementation? Short answer: no. Is it really cool and sexy though? Yes.

How EPiServer CMS manages cache works great with how the platform can be deployed today. Tomorrow, with more dynamic environments, you may want to return here and check out how to implement the Windows Azure AppFabric Cache service together with EPiServer CMS.

Thanks for reading!

daniel
daniel
Developer
Recent Posts
Contact Us

We're not around right now. But you can send us an email and we'll get back to you, asap.

Not readable? Change text. captcha txt

Start typing and press Enter to search