SharePoint 2010 / 2013: Using Distributed Cache in your server-side code

Distributed cache, aka AppFabric cache is an independent component that can be installed as a part of Windows Server OS.

In SharePoint 2010 it was not an integral part of SharePoint, however developers were still able to utilize that in they code. Wictor Wilen has an article that describes how to use the AppFabric caching API in SP2010. API itself remains unchanged between SP2010 and SP2013. However, since AppFabric caching is now an integral part of SP2013, we can streamline the code to work better within the SharePoint context. For example, SharePoint can tell us all the available host endpoints.

Please refer to Bernado Nguyen-Hoan’s post where he describes the CacheManager class that can be later used by your code to consistently store and retrieve data. The code is multi-WFE proofed and is overall hassle free.

The CacheManager class below allows you to access the default cache created by SharePoint (as an instance of the DataCache class).

The SPDistributedCacheClusterInfoManagerand SPDistributedCacheClusterInfo classes are used to retrieve all the cache host endpoints. These classes are in the Microsoft.SharePoint.DistributedCaching.Utilities namespace.

Code itself:

 

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *