SharePoint 2013 – Adding my custom object in Distributed Cache using AppFabric client DLLs
Hi All,
Today lets discuss related to Distributed Cache Service of SharePoint 2013. Recently I got a chance to deliver the SharePoint 2013 training in my company and one of the topic was Distributed Cache Service. I did research, read some good posts so that I can understand it thoroughly.
After doing some research and reading from MSDN and some good posts, I was little confused and following two questions are coming frequently in my mind
1. Whether Distributed Cache replaces existing cache techniques (Object Cache, Blob Cache and Output Cache)
2. How programmatically add my objects / data into Distributed Cache
So here I am trying to explain the above two points.
Answer to first question is NO. Distributed Cache Service doesn’t replace the existing caching techniques. Distributed Cache Service provides the additional cache technique further than above mentioned options.
SharePoint 2013 uses 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 – http://technet.microsoft.com/library/jj219700(office.15).aspx Now answer to second question, Unfortunately SharePoint doesn’t itself provide the APIs for adding / removing my objects to Distributed Cache. But there is workaround for this. Workaround is using AppFabric client DLLs.
To demonstrate 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. Two client DLLs which we require are
Microsoft.ApplicationServer.Caching.Client.dll
Microsoft.ApplicationServer.Caching.Core.dll
So those references needs to be added to our solution as
Fig 1. SharePoint 2013 : Distributed Cache Service – Storing custom object in cache
3. Since AppFabric cache cluster uses named cache to store cache items we need to get the named cache which 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 instance DataCacheFactoryConfiguration as
I have put comment so that those will be easily get understood:) //DataCacheFactory – Provide methods to return DataCaching Object which is mapped to named cache
DataCacheFactory dataCacheFactory = null;
//DataCacheServerEndpoint - Represent each cache host List<DataCacheServerEndpoint> servers = new List<DataCacheServerEndpoint>();
//Host name and Port number servers.Add(new DataCacheServerEndpoint("My Cache Host", 22233));
DataCache cache = null;
DataCacheFactoryConfiguration configuration = new DataCacheFactoryConfiguration();
//Sets the length of time that the cache client waits to establish network connection with server configuration.ChannelOpenTimeout = TimeSpan.FromSeconds(45);
configuration.IsCompressionEnabled = false;
//Maximum number of channels to be open to the cache cluster configuration.MaxConnectionsToServer = 1;
//Sets the length of time that the cache client waits for response from the server for each request configuration.RequestTimeout = TimeSpan.FromSeconds(45);
//Array of DataCacheServerEndpoints object. Each EndPoint represent the cache host configuration.Servers = servers;
configuration.TransportProperties.MaxBufferSize = 1000000;
dataCacheFactory = new DataCacheFactory(configuration);
//Get the default cache
cache = dataCacheFactory.GetDefaultCache()
Once we get the DataCache instance, we can add / remove our cache instances as
cache.Add(CACHE_KEY, object); //Object is any serializable entity
datacache.Remove(CACHE_KEY);
Above we are getting default cache, you will get the whole list of different named cache which SharePoint 2013 uses from http://technet.microsoft.com/library/jj219700(office.15).aspx
Thanks for reading 🙂
Keep reading, share your thoughts, experiences. Feel free to contact us to discuss more. If you have any suggestion / feedback / doubt, you are most welcome.
Stay tuned on Knowledge-Junction, will come up with more such articles