Playing with Distributed Cache
I just got around to play with Distributed Cache (Project Velocity) in .NET and I am amazed how easy it is to use such a storage and it puzzles me why didn't we have this available before. Here is a short description of what I was playing around with.
1. First you have to install the distributed cache on one (or more) servers (can be a cluster configuration or any other option you desire). With installation you have to choose where you want to store your cache configurations (central location: can be SQL or file share). On the server you get a number of Windows PowerShell cmdlets that let you do the work:
PS ...\Microsoft Distributed Cache\V1.0> start-cachecluster
HostName : CachePort Service Name Service Status
-------------------- ------------ --------------
MYCOMPUTER:22233 DistributedCacheService SERVICE UP
PS ...\Microsoft Distributed Cache\V1.0> get-cachehost
HostName : CachePort Service Name Service Status
-------------------- ------------ --------------
MYCOMPUTER:22233 DistributedCacheService SERVICE UP
PS ...\Microsoft Distributed Cache\V1.0> get-cache
CacheName [Host]
Regions
--------- ---------------
default
PS ...\Microsoft Distributed Cache\V1.0> new-cache -CacheName MyCache
PS ...\Microsoft Distributed Cache\V1.0> get-cache
CacheName [Host]
Regions
--------- ---------------
default
MyCache
2. After that you have to set-up your dev machine which was in my case the same as the cache server. It basically means you have to get proper references to DLLs in your project and start using System.Data.Caching namespace. Additionally you have to properly define the app.config file so that the CacheFactory object can instantiate properly. It is basically copy and paste action with a bit of tweaking and one (pretty important part) of the xml file (app.config) looks like the following (for the upper cache setup):
<hosts>
<!--List of hosts -->
<host
name ="MYCOMPUTER"
cachePort="22233"
cacheHostName="DistributedCacheService"/>
</hosts>
Be alert about the cacheHostName since it is a bit misleading. It actually has to refer to cache service and not cache host and that can easily save you a couple of minutes :). You could of course do all this in code instead of config file but I always opt for the config files due to easier deployment and configurability.
3. Start using it and it is pretty simple. For example a couple of code lines that will get you started look like this:
// cache key/identifier
string cItem = "1234";
// initializing CacheFactory as defined in app.config
myCF = new CacheFactory();// getting cache
Cache myCache = myCF.GetCache("MyCache");// adding or retrieving items
myCache.Put(cItem, "Some Object");
myCache.Get(cItem);
I tried it out a bit more and did a simple WinForms project to write and store items in the cache and then ran 2 instances in the same time, retrieving and storing items in cache. With that you can pretty soon get the feeling of what this technology can mean and how it can help you develop distribute applications where data is spread across tiers and you usually need some temporary storage for it.
What is even greater is the fact that you can easily configure IIS/ASP.NET apps to use this cache as the session state cache as well. Neat, ha?
More info on: https://msdn.microsoft.com/en-us/data/cc655792.aspx.