Object Caching - .NET 4

As a developer, we need to use all the tools at our disposal to develop faster and more robust applications. One of the ways we can achieve this is by using caching. Caching is the process of storing frequently used data on the server to fulfil subsequent requests. Now there are a few different types of caching available to the .NET developer, but in this article I am going to discuss data caching using the object cache. The cache object enables you to store everything from simple name / value pairs to more complex objects such as datasets and entire pages.

Previously, the cache object used to fall under the System.Web.Caching.Cache namespace. Due to it's power and awesomeness, many developers would copy this namespace into their projects and use it in other applications such as Windows Forms and WPF apps. With the release of .NET 4, the namespace has been refactored and totally rebuilt into the new namespace of System.Runtime.Caching. The old namespace is still available, but any new enhancements will be added to the System.Runtime.Caching namespace.

In this example, I am going to take a look at Object Caching. For more types of caching available to you in the .net framework - check out this link. Now, let's look at a code example and run through how easy it is to add some caching to your application. I'm going to use an example of some of the caching I use when building on this site.

To start, you add a reference to the System.Runtime.Caching object in your project.

Add Reference

If you wanted to add an item to the cache, you would need to do the following:

Add to Cache

In this line, we are obtaining a reference to the default MemoryCache instance. Using the new .NET Caching runtime, you can have multiple MemoryCaches inside a single application.

ObjectCache Cache = MemoryCache.Default;

Then we add the object to the cache along with a key. The key allows us to retrieve this at a later point.

Sometimes you might come across a scenario where a similar object will have similar name, in this case you might want to build a dynamic key string to identify your objects.

Cache.Add("Key", objectToCache, DateTime.Now.AddDays(30));

Retrieving our object is just as simple, but this time let's make a re-usable method that is a lot sexier.

For this example I have added the above method to a class called CacheLayer, and if we wanted to call the method that has just been created:

Cache Get

Let's put the whole thing together in a real world example and show the power of caching.

Cache Example

In the above example, we check if the cache contains the data that we are looking for based on the key. If it doesn't contain the data, we retrieve it from the Database and then add it to the cache. Then the next time that the method is called it won't have to hit the Database, but simply get the data from memory. This saves a lot of time and overhead!

NOTE: If you make changes on your application, you might not see them immediately because they might be in cache. You might need to remove the object in order to see your changes - you can always add them back in!

In order to wrap all this up, I have created a little class that you can easily call and use for your caching needs. If you would like to download just the class file - click here

Else if you prefer to download the full class library project with working unit tests

Download