Boost your SharePoint solutions with distributed cache!–Part 3–Let’s Code!

Dec 17, 2013

Let’s code!

All what you need is located in these namspaces Microsoft.SharePoint.DistributedCaching , Microsoft.SharePoint.DistributedCaching.Utilities, Microsoft.ApplicationServer.Caching, it is strictly forbidden to reference anything else coming from the appfabric sdk!

Another information, depending on your version of SharePoint and of Visual Studio it is possible that you don’t have these dll’s in SharePoint’s ISAPI (folders VS will look at to know which assemblies are available for your projects). In that case you will need to reference dll’s directly from their GAC location. You can use ILSPY or something equivalent to know the exact location. (or ask me in comments if you need it).

Because I’m a very kind developer, you will find attached a cs file which will help you to interact with the cache. This is not very complicated, cache cluster relies on a factory pattern. Talking about that, in order to do things properly, you will have to dispose factories properly at the end of the request (global.asax for example) and/or at the end of your process (in the case of a timerjob).

Once the factory is instanciated, we obtain the cache corresponding to the targeted partition (same thing, DataCache is not disposable, but if you dispose factories, think about un-referencing these objects too). That done, you only have to use the cache via the Microsoft provided methods.

Note that I’ve done for myself a method called PutAndCheckRegion. Indeed, regions disappear when they do not contain any more object, you have to ensure a region is here before storing something into it.


I hope you learnt plenty of things about this SharePoint provided caching solution. Again, this is one of all available solutions, it can be useful or not depending on your needs. I tried to give a maximum of information so you have everything you need to make clever decisions.

No more excuses to have slow SharePoint solutions now! If you have any question/remark/complain, feel free to comment.

Edité la dernière fois le 27 May 2021 par Vincent Biret