How to Configure .NET 4.0 Cache to use a Distributed Cache?

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.

Download NCache free trial - Extremely fast and scalable in-memory distributed cache

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.

  1. Create a Clustered (distributed) cache through GUI based NCache Manager. I created a clustered cache named as “MyClusterCache”.
  2. Start the cache to make it ready to use.
  3. Add references of  Alachisoft.NCache.ObjectCacheProvider library to your application from “NCacheInstallDir/NCache/integration/DotNet4.0 Cache Provider
  4. Include the following namespace in your project.
    using Alachisoft.NCache.ObjectCacheProvider;
    
  5. 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);
    
    
  6. 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.

Download NCache Trial | NCache Details

This entry was posted in .NET 4.0 Cache, Distributed Cache, Distributed caching and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


+ 5 = eight

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>