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

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

Advertisements

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.