Adding A Custom Object To Distributed Cache Using AppFabric Client DLLs

Hello All.

This is related to the Distributed Cache Service of SharePoint 2013. I got a chance to deliver the SharePoint 2013 training and one of the topics was Distributed Cache Services. I did a little research and read some good posts so that I can understand it thoroughly.

After doing some research and reading from the MSDN and some good posts, I was a little confused and the following two questions were frequently in my mind.

  1. Whether Distributed Cache replaces the existing cache techniques (Object Cache, Blob Cache and Output Cache).

  2. How to programmatically add my objects / data into a Distributed Cache.

So here I am trying to explain the preceding two points.

The answer to first question is no. The Distributed Cache Service doesn't replace the existing caching techniques. The Distributed Cache Service provides an additional cache technique beyond the preceding options.

SharePoint 2013 uses the Distributed Cache Service for some of the features, listed below:

  1. Authentication
  2. Newsfeeds
  3. OneNote client access
  4. Security Trimming
  5. Page load performance

Overview of Distributed Cache service.

Now the answer to the second question is, unfortunately SharePoint doesn't itself provide the APIs for adding objects to and removing objects from a Distributed Cache. But there is a workaround for this. The workaround is to use AppFabric client DLLs.

To show this, let's start step-by-step.

  1. The AppFabric client DLLs are deployed when SharePoint prerequisites are deployed in \Program Files\AppFabric 1.1 for Windows Server\.

  2. The two client DLLs that we require are:

    1. Microsoft.ApplicationServer.Caching.Client.dll
    2. Microsoft.ApplicationServer.Caching.Core.dll

    So those references must be added to our solution as in the following:

    references

  3. Since an AppFabric cache cluster uses a named cache to store the cache items we need to get the named cache that is of type DataCache. Here in this example I am using the default cache.

    [Named Cache – Container for our cache objects / items]

  4. To get the default named cache we need to provide the configurations settings using an instance of DataCacheFactoryConfiguration as in the following.

    I have included comments to make the code easily understandable.
    1.    //DataCacheFactory - Provide methods to return DataCaching Object which is mapped to named cache  
    2.   DataCacheFactory dataCacheFactory = null;  
    3.     
    4. //DataCacheServerEndpoint  - Represent each cache host  
    5. List<DataCacheServerEndpoint> servers = new List<DataCacheServerEndpoint>();  
    6.   
    7. //Host name and Port number  
    8. servers.Add(new DataCacheServerEndpoint("My Cache Host", 22233));  
    9. DataCache cache = null;  
    10.   
    11. DataCacheFactoryConfiguration configuration = new DataCacheFactoryConfiguration();  
    12.   
    13. //Sets the length of time that the cache client waits to establish network connection with server  
    14. configuration.ChannelOpenTimeout = TimeSpan.FromSeconds(45);  
    15. configuration.IsCompressionEnabled = false;  
    16.   
    17. //Maximum number of channels to be open to the cache cluster  
    18. configuration.MaxConnectionsToServer = 1;  
    19.   
    20. //Sets the length of time that the cache client waits for response from the server for each request  
    21. configuration.RequestTimeout = TimeSpan.FromSeconds(45);  
    22.   
    23. //Array of DataCacheServerEndpoints object. Each EndPoint represent the cache host  
    24. configuration.Servers = servers;  
    25. configuration.TransportProperties.MaxBufferSize = 1000000;  
    26.   
    27. dataCacheFactory = new DataCacheFactory(configuration);  
    28.   
    29. //Get the default cache  
    30. cache = dataCacheFactory.GetDefaultCache();  
  5. Once we get the DataCache instance, we can add or remove a cache instance as in the following:
    1. cache.Add(CACHE_KEY, object); //Object is any serializable entity  
    2.   
    3. datacache.Remove(CACHE_KEY);  
  6. In the preceding we are getting the default cache, you will get the entire list of named caches that SharePoint 2013 uses from: Overview of microblog features, feeds, and the Distributed Cache service in SharePoint Server 2013.

Thanks.