Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Using Membase for ASP.NET Output Caching

0.00/5 (No votes)
18 Apr 2011 1  
Using Membase for ASP.NET Output Caching

Membase is no-SQL, distributed, key-value database management system for storing data offered by NorthScale.

Download and Installation of Membase

Membase community edition can be downloaded from here. Its installation is pretty straightforward.

Membase Client

Membase has client support available in C# and the client support libraries can be downloaded from here.

ASP.NET Project

Create a new Visual Studio ASP.NET Web Application. Add Membase.dll and Enyim.Caching.dll as references to the project.

Initializing Membase from Website

Open web.config for the application and the following section under configSections:

fig1.png

Change the URI to point to your Membase server. If you are having a clustered environment, add multiple (Check Membase demo sample). Next step would be to initialize the connection between the Membase and website. Open Global.asax file and add the following code block at Application Start event.

fig2.png

Note: Unity Framework is used to manage the lifetime of MembaseClient object.

MembaseOuptputCacheProvider: Extension Class for Output Cache

In .NET Framework 4.0, Microsoft provided us the ability to extend the Output Cache and use our own custom Output Cache mechanism for the application. To implement a new OutputCache provider, we need to implement OutputCacheProvider class. This class defines four abstract methods:

  • Add: key, object value and expiry time is passed to the method. However if the key already exists in the cache, one has to return the value associated with it (imp).
  • Get: It returns the object associated with the key.
  • Remove: Remove the key associated from the cache.
  • Set: Add/Update the key in cache, and its expiry.

Following is the implementation for the class:

fig3.png

The above is the implementation for subclass MembaseCacheProvider. The constructor uses the Unity Framework to get a reference to the MembaseClient.

The Add method does a look up for the key. If the key is found in Membase, it returns the value associated, else the value is stored in Membase for the given expiry time. Membase provides Store method for saving/updating the items in cache. To Add a new key to Membase, StoreMode.Add is passed to method.

The Get method does a look up for the key. If the key is not found in the cache, it returns null.

The Remove method calls the remove method of Membase client and removes the value from the key.

The Set method does a look up for the key. If the item is found in store, it does the update for item, else it Adds it as new item.

Setting Custom Provider as Default

In web.config file, add the following section under System.web:

fig4.png

This block of code tells .NET to register the Custom Output Cache as the default cache mechanism for the application.

Last Step: Testing

To test, I would do the basic output cache based on param. Let's create a page and the following Output Cache Directive on the top of page.

ASP.NET
<%@ OutputCache VaryByParam="Test" Duration="60000? %>

And add the following block in the Code behind:

C#
Response.Write(System.DateTime.Now.ToString());

Run the application and we can see the caching works fine.

If we check the Membase console and take a look at the top keys for default bucket, we will see the entries done by .NET Framework.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here