How to use SharePoint 2013’s AppFabric caching in your code

By default SharePoint 2013 installs an AppFabric cache host on all servers in the farm. These hosts form a cache cluster for the farm.

In SP2010 if you were running a multi-WFEs farm and using in-process caching, your users will randomly see difference data – because they are randomly being taken to difference WFEs, and each WFE maintains a different local in-process cache. If you enable server-affinity, then a given user will always see the same data. However, he/she may not see the same data as another user. AppFabric caching in SP2013 should solve that problem.

Wictor Wilen has an article that describes how to use the AppFabric caching API in SP2010. The AppFabric 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.

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

The SPDistributedCacheClusterInfoManager and SPDistributedCacheClusterInfo classes are used to retrieve all the cache host endpoints. These classes are in the Microsoft.SharePoint.DistributedCaching.Utilities namespace. Review the in-code comments.

public sealed class CacheManager
    {
        private static readonly object _lock = new object();
        private static DataCache _defaultCache;

        private CacheManager() {}

        public static DataCache DefaultCache
        {
            get
            {
                using (SPMonitoredScope scope = new SPMonitoredScope("CacheManager.DefaultCache"))
                {
                    lock (_lock)
                    {
                        if (_defaultCache == null)
                        {
                            //By default the farm ID is appended to the name of the cluster and caches
                            //created by SharePoint.
                            var farmID = SPFarm.Local.Id;

                            //This is one of the caches created by SharePoint. Run the AppFabric's
                            //Get-Cache PowerShell to see a list of caches created by SharePoint.
                            var defaultCacheName = "DistributedDefaultCache_" + farmID;

                            var factoryConfiguration = new DataCacheFactoryConfiguration()
                            {
                                Servers = GetAllDataCacheServerEndpointsForFarm(farmID)
                            };

                            _defaultCache = new DataCacheFactory(factoryConfiguration).GetCache(defaultCacheName);
                        }
                        return _defaultCache;
                    }
                }
            }
        }

        private static List<DataCacheServerEndpoint> GetAllDataCacheServerEndpointsForFarm(Guid farmID)
        {
            var endpoints = new List<DataCacheServerEndpoint>();

            //By default this is the name of the cluster created by SharePoint.
            string cacheClusterName = "SPDistributedCacheCluster_" + farmID;

            var cacheClusterManager = SPDistributedCacheClusterInfoManager.Local;
            var cacheClusterInfo = cacheClusterManager.GetSPDistributedCacheClusterInfo(cacheClusterName);

            foreach (var cacheHost in cacheClusterInfo.CacheHostsInfoCollection)
            {
                endpoints.Add(new DataCacheServerEndpoint(cacheHost.HostName, cacheHost.CachePort));
            }
            return endpoints;
        }
    }

Once you have a reference to an instance of DataCache, you can use the normal AppFabric caching API to insert and retrieve items from the cache. Refer to Wictor’s article above for some examples.

Advertisements

About Bernado

Based in Australia, I am a freelance SharePoint and Dynamics CRM developer. I love developing innovative solutions that address business and everyday problems. Feel free to contact me if you think I can help you with your SharePoint or CRM implementation.
This entry was posted in AppFabric, SharePoint. Bookmark the permalink.

7 Responses to How to use SharePoint 2013’s AppFabric caching in your code

  1. Steve says:

    Hey, nice post. Interesting problem i have!
    When adding a reference in VS, I can’t see AppFabric folder inside SysNative (i also tried simply typing it in the address bar).
    I do have have the dlls in this directory … C:\Program Files\AppFabric 1.1 for Windows Server

    Do you know if they are the same as what I require for using AppFabric in SP 2013?

    Thx

  2. Cj says:

    Is this still true? Do you absolutely need a New cache cluster?

  3. Pingback: SharePoint 2010 / 2013: Using Distributed Cache in your server-side code – Journeyman's Cradle

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s