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.
- Whether Distributed Cache replaces the existing cache techniques (Object Cache, Blob Cache and Output Cache).
- 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:
- Authentication
- Newsfeeds
- OneNote client access
- Security Trimming
- 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.
- The AppFabric client DLLs are deployed when SharePoint prerequisites are deployed in \Program Files\AppFabric 1.1 for Windows Server\.
- The two client DLLs that we require are:
- Microsoft.ApplicationServer.Caching.Client.dll
- Microsoft.ApplicationServer.Caching.Core.dll
So those references must be added to our solution as in the following:
- 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]
- 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.
-
- DataCacheFactory dataCacheFactory = null;
-
-
- List<DataCacheServerEndpoint> servers = new List<DataCacheServerEndpoint>();
-
-
- servers.Add(new DataCacheServerEndpoint("My Cache Host", 22233));
- DataCache cache = null;
-
- DataCacheFactoryConfiguration configuration = new DataCacheFactoryConfiguration();
-
-
- configuration.ChannelOpenTimeout = TimeSpan.FromSeconds(45);
- configuration.IsCompressionEnabled = false;
-
-
- configuration.MaxConnectionsToServer = 1;
-
-
- configuration.RequestTimeout = TimeSpan.FromSeconds(45);
-
-
- configuration.Servers = servers;
- configuration.TransportProperties.MaxBufferSize = 1000000;
-
- dataCacheFactory = new DataCacheFactory(configuration);
-
-
- cache = dataCacheFactory.GetDefaultCache();
- Once we get the DataCache instance, we can add or remove a cache instance as in the following:
- cache.Add(CACHE_KEY, object);
-
- datacache.Remove(CACHE_KEY);
- 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.