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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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.
1using Alachisoft.NCache.ObjectCacheProvider; - Initialize your CacheProvider (inherited from ObjectCache) and pass your cache name to the provider as shown below.
1234ObjectCache _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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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.
Hey Hi!
NCache ObjectCacheProvider for .Net 4.0 Cache has been officially discontinued and therefore the assembly Alachisoft.NCache.ObjectCacheProvider is no longer shipped with NCache installation. This is because Caching Application Block functionality is now built into .NET Framework 4.0 and Enterprise library is deprecated in releases after .Net 5.0. The current .NET Caching support doesn’t come with a provider model which limits 3rd party integrations such as NCache to plug in.
You can use the NCache object caching(Application data caching) API for caching objects in NCache by following the guidelines in the below link:
https://www.alachisoft.com/resources/docs/ncache/help/basic-cache-operations.html?mw=MjQw&st=MQ==&sct=MA==&ms=QwAAEAAAAAAAAAACASgE
Hi Iqbal,
I am trying to implement the code provided in the blog ‘How to Configure .NET 4.0 Cache to use a Distributed Cache?’
I downloaded trial version of NCache Enterprise 4.6.
I have to add references of Alachisoft.NCache.ObjectCacheProvider library to the application from “NCacheInstallDir/NCache/integration/DotNet4.0 Cache Provider“ but I can’t see Alachisoft.NCache.ObjectCacheProvider.dll under integration folder in my installation directory.
Can you please guide me further where can I find Alachisoft.NCache.ObjectCacheProvider.dll?
Is this functionality available in Ncache 4.6? If yes, how to achieve it?
Hope to hear from you as soon as possible.
Thanks,
Sejal