Month: April 2012

NCache with .NET 4

I am very excited about this new series I am publishing today. It’s all about caching, a very useful portion of your architecture that should be seriously taken into consideration, especially if you are designing a web application.

Actually, I used NCache with NHibernate and I can only say that it is a very good and valuable product. Of course most of you may believe that I got a free license so this is the reason of my review. Actually I am reviewing this product because I found it pretty good and that’s why the review came up with a short series of articles.

I will show you the following features of NCache:


From NCache documentation:

NCache is a clustered caching solution that makes sharing and managing data in a cluster as simple as on a single server. It accomplishes this by coordinating updates to the data using cluster-wide concurrency control and replicating and distributing data modifications across the cluster using the highest performing clustered protocol available. The primary purpose of NCache is to help improve performance of .NET applications that would otherwise make expensive trips to database systems, web services, mainframes, or other systems across the network.

Below is a very simplified diagram of NCache architecture (please forgive me for the simplicity of this architecture design):


So, how does it work?

The explanation is pretty simple but in the same time is represented by a complex architecture. You can have two different type of Cache, a local cache or a remote cache. The first one will work because the client has a cache server installed locally, the second one will work with the client accessing the remote cache server from the network.


Of course you can easily swap from a local configuration to a remote configuration without even need to restart or rebuild your application, plus you can easily add or remove cache clusters to your architecture using an “hot swap” technology that doesn’t require any reboot or re-configuration.


Let’s try to get practical and let’s write some code, otherwise this blog post will get really boring! Winking smile

In order to use NCache you have to go to this web address and download a 60 days trial version: There are three major versions: NCache for .NET (x86 and x64) in two flavors, Enterprise and Professional, NCache for Java (x86 and x64) and NCache express (a free version with a limited amount of features). In this demo I will use the full version for .NET in x64 bit Professional. The setup for the professional edition is about ~30 Mb and it’s pretty quick to install.

Note: the only problem you may encounter is with Windows UAC because NCache works better if UAC is disabled. To know more about UAC go here: MSDN Windows ACL

The Wizard will propose you three different types of installation, like the screenshot below:


In my case I need a developer license because I will run everything from my machine and I need also to have access to NCache API. The cache server installation is used to install a new cluster while the Remote Client installation is used to install the client connection components used by your applications to access the remote cache servers.

As soon as NCache is installed you will get a new icon in your start menu (in my case is a tile because I have Windows 8):


In the next posts I will show you how to configure your first cache and how to write a WPF application that will work against this cache.

Stay tuned

NCache, simple caching of objects.

In the previous post I have introduced you to NCache architecture. In this post we will create our first cache (locally) and we will write some C# code to interact with this cache.

For the purpose of this series I have created a very simple WPF 4.0 application available from my SkyDrive (see below at the end of this post) that you can download and run locally; but of course you need to install NCache in your development machine.


With this very simple WPF application (even if very simple it works with IoC and MVVM) I have a Listbox that will provide a list of items (books titles). The list will be retrieved with the first command from a database, with the second command from the cache and with the third command the cache will be cleared. The cache is populated automatically after you retrieve the data from the database.

Note: I am including the full source code of this series but this is not a series about MVVM and/or WPF so I will not provide that code inside the posts in order to stay focus on the topic, which is NCache.

Create a new local cache

First of all we need to create a new cache in NCache and this can be easily accomplished using the UI of NCache. Open you instance of NCache and create a new local cache, which is going to be more than enough for our demo.

The following picture shows the steps you need to follow to create a default local cache in your system.


  1. Create a new local cache
  2. Select the current machine as the destination of the local cache
  3. Name the cache
  4. Configure the memory allowed by this cache
  5. Configure the objects lifecycle for the cache

At this point, NCache will create the new cache and if you have configure it to startup the cache right away, the NCache IDE will open a new window showing you the NCache health status. From this window you can monitor the amount of objects created by the cache and many other functionalities.

Retrieve the data from the database

In this project I have created a very simple database structure that contains a table with a bunch or rows, each row represents a Book record. The data is retrieved using Entity Framework and this is the code used to populate the Listbox:

   1: public IList<Book> GetListOfBooks()

   2: {

   3:     using (var orm = new ORM())

   4:     {

   5:         var results = orm.GetListOfBooks();

   6:         return results

   7:             .Select(result => 

   8:                 new Book {Title = result.Title, ISBN = result.ISBN})

   9:                 .ToList();

  10:     }

  11: }

The data is then DataBound to a WPF listbox using the MVVM pattern:

   1: <ListBox 

   2:    Grid.Column="0" Grid.Row="1" Grid.RowSpan="3" 

   3:    Style="{StaticResource ListboxStyle}" 

   4:    ItemsSource="{Binding Books,IsAsync=True}">

   5:     <ListBox.ItemTemplate>

   6:         <DataTemplate>

   7:             <StackPanel>

   8:                 <TextBlock Text="{Binding ISBN}" />

   9:                 <TextBlock Text="{Binding Title}" />

  10:             </StackPanel>

  11:         </DataTemplate>

  12:     </ListBox.ItemTemplate>

  13: </ListBox>

And the listbox shows the final results:


Retrieve the data from the cache

The second command of the UI is calling the cache, instead of calling the database, to retrieve the data. But, how does it work? I have drawn below a very simple logical flow that my application follows in order to retrieve the data from the cache, when available.


And this is how it works:

   1: // initialize the cache

   2: using (Cache cache = NCache.InitializeCache("Cache"))

   3: {

   4:     var list = new List<Book>();

   5:     // if cache empty, populate the cache

   6:     if (cache.Count == 0)

   7:     {

   8:         IList<Book> books = GetListOfBooks();

   9:         foreach (Book book in books)

  10:         {

  11:             cache.Add(book.ISBN, book);

  12:         }

  13:     }

  14: }

Of course this is just a simple implementation to show you how the NCache mechanism works. It is like working with a simple dictionary of objects if you are not implementing NCache on top of another caching framework.

As soon as you add the items to the cache, the NCache management studio will show you the new objects cached in the system:


Few final notes

  • First of all you need to orchestrate your application in a way that the cache will be bootstrapped with the application, bootstrapping the cache in a method of the data layer like I did it is absolutely wrong!
  • The objects contained inside the cache, like any other cache system, must be serializable otherwise you will receive a nice runtime exception from NCache.