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 has client support available in C# and the client support libraries can be downloaded from here.
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
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.
Note: Unity Framework is used to manage the lifetime of
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
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 (
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:
The above is the implementation for subclass
MembaseCacheProvider. The constructor uses the Unity Framework to get a reference to 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.
Get method does a look up for the key. If the key is not found in the cache, it returns
Remove method calls the
remove method of Membase client and removes the value from the key.
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:
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.
<%@ OutputCache VaryByParam="Test" Duration="60000? %>
And add the following block in the Code behind:
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.