In-memory distributed cache today has become really popular for applications running in a multi-server environment because it helps improve application scalability and performance. Until .NET Framework 3.5 there was ASP.NET Cache object available only for web application under System.Web.Caching namespace. But, in .NET Framework 4.0, .NET 4.0 Cache is added under System.Runtime.Caching namespace for all types of .NET applications. .NET 4.0 Cache has functionality similar to ASP.NET Cache. But, unlike ASP.NET Cache, it has an abstract class ObjectCache that can be implemented in a customized way as needed. So, in essence .NET 4.0 Cache can be extended which ASP.NET Cache cannot be. And, MemoryCache is the default in-memory cache implementation of .NET 4.0 Cache. Here is an example:
private static ObjectCache cache = MemoryCache.Default; private CacheItemPolicy policy = null; private CacheEntryRemovedCallback callback = null; // Registering callbacks and policies… callback = new CacheEntryRemovedCallback(this.MyCachedItemRemovedCallback); policy = new CacheItemPolicy(); policy.Priority = (MyCacheItemPriority == MyCachePriority.Default) ? CacheItemPriority.Default : CacheItemPriority.NotRemovable; policy.RemovedCallback = callback; HostFileChangeMonitor changeMonitor = new HostFileChangeMonitor(FilePath); policy.ChangeMonitors.Add(changeMonitor); // Add inside cache… cache.Set(CacheKeyName, CacheItem, policy);
One limitation of .NET 4.0 Cache’s default implementation MemoryCache is that it is a stand-alone in-process cache. If your .NET application runs on a multi-server environment, then you cannot use this because you need a distributed cache that can synchronize the cache across multiple servers. But fortunately, .NET 4.0 Cache architecture allows us to plug in a third party distributed cache solution and extend it.
To address this need, Alachisoft has implemented an easy to use .NET 4.0 Cache Provider that can solve data synchronization, distribution and scalability issues especially in case of web farm/garden. This provider basically integrates NCache with .NET 4.0 Cache. NCache is a very popular enterprise level distributed cache for .NET. Through NCache’s .NET 4.0 Cache Provider you can plug in NCache with your application to achieve the benefits of a distributed cache. Let me show you how easily it can be done with NCache by a few steps.
- Create a Clustered (distributed) cache through GUI based NCache Manager. I created a clustered cache named as “MyClusterCache”.
- Start the cache to make it ready to use.
- Add references of Alachisoft.NCache.ObjectCacheProvider library to your application from “NCacheInstallDir/NCache/integration/DotNet4.0 Cache Provider“
- Include the following namespace in your project.
- Initialize your CacheProvider (inherited from ObjectCache) and pass your cache name to the provider as shown below.
ObjectCache _cache; string _cacheId = "MyClusterCache" ; _cache = new CacheProvider(_cacheId);
- Now you can perform all cache related operations on your cache using CacheProvider commands.
Here is full example of .NET 4.0 extended for NCache:
ObjectCache _cache; string _cacheId = "MyClusterCache" ; // Initialize with NCache’s .NET 4.0 Cache Provider. _cache = new CacheProvider(_cacheId); // Registering callbacks and policies… NCacheFileChangeMonitor changeMonitor = new NCacheFileChangeMonitor(fileNames); CacheItemPolicy ciPolicy = new CacheItemPolicy(); ciPolicy.ChangeMonitors.Add(changeMonitor); ciPolicy.RemovedCallback += new CacheEntryRemovedCallback(onCacheEntryRemoved); //Add the dependent items in the cache. _cache.AddItems(ciPolicy, 0, totalKeys);
NCache implementation of .NET 4.0 Cache also includes custom implementation of ChangeMonitor as NCacheEntryChangeMonitor, NCacheFileChangeMonitor, NCacheSqlChangeMonitor and NCacheOracleChangeMonitor for entry, file, SQL and Oracle based changes respectively. Through NCache’s implementation of .NET 4.0 Cache interface, you can now adopt .NET 4.0 Cache as your standard and at the same time benefit from an enterprise level distributed cache for your .NET applications running in a multi-server environment.