Click here to Skip to main content
15,885,737 members
Articles / Web Development / ASP.NET

Using Membase for ASP.NET Output Caching

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
17 Apr 2011CPOL3 min read 13.1K   2  
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer SAP Labs
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --