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

Memcached (Distributed Cache) ASP.NET Provider

Rate me:
Please Sign up or sign in to vote.
3.54/5 (13 votes)
31 Dec 2007Apache2 min read 144.6K   104   57   19
Provider for using Memcached in a web farm

Introduction

Currently, I am working on a web-based project that will be hosted in a web farm. Among several issues, we faced the issue of having a distributed cache. As we know, the current cache in ASP.NET is an in-process cache and can't be used in a web farm. After doing some research, we found a few solutions on the web but at the end, all of them had scalability issues. We found a few third party implementations but they were quite expensive. Then I came across the Memcached implemented by Danga Interactive. This is a high performance distributed memory cache initially implemented for http://www.LiveJournal.com. The original implementation runs on a *nix system. Luckily, there is Win32 port available at http://jehiah.cz/projects/memcached-win32/ for those who want to run it in a Windows environment. For more details on the working of Memcached, please read this article.

There is a C# client for Memcached available and can be downloaded from here. For coding this provider, I have used clientlib 1.1.5.

Using the Code

Following is the interface for the Cached Provider:

C#
public abstract class CacheProvider : ProviderBase
{
    // Currently returns -1 as this property is not implemented
    public abstract long Count { get;}
    // Returns the server names with port in the memcached cluster
    public abstract string[] Servers { get;}
    // Default expiration time in milli seconds
    public abstract long DefaultExpireTime { get;set;}
    // Adds object to the cache for the max amount of time
    public abstract bool Add(string strKey, object objValue);
    // Adds object to the cache for Default Expire time if bDefaultExpire
    // is set to true otherwise for max amount of time
    public abstract bool Add(string strKey, object objValue,bool bDefaultExpire);
    // Add objects for specified about of time. Time is specified in milli seconds        
    public abstract bool Add(string strKey, object objValue, long lNumofMilliSeconds);
    // Return the object from memcached based on the key otherwise 
    // just returns null
    public abstract object Get(string strKey);
    // Clears everything from the cache on all servers
    public abstract bool RemoveAll();
    // Removes the an object from cache
    public abstract object Remove(string strKey);
    // Release resources and performs clean up
    public abstract void Shutdown();
    // Checks if the key exists in memory
    public abstract bool KeyExists(string strKey);
    // Return all servers stats
    public abstract Hashtable GetStats();
}

Following is how to configure the Cache Provider in a web.config or app.config file:

XML
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

 <configSections>
  <section name="cacheProvider"
      type="CacheProvider.CacheProviderSection, CacheProvider"
      allowDefinition="MachineToApplication" 
      restartOnExternalChanges="true"/>
 </configSections>

 <cacheProvider defaultProvider="MemcachedCacheProvider">
  <providers>
   <add name="MemcachedCacheProvider" 
       type="CacheProvider.MemcachedCacheProvider,CacheProvider"
       servers="127.0.0.1:11211" 
       socketConnectTimeout="1000" 
       socketTimeout="1000"/>

   <!—To add more servers use comma to separate servernames and ports
   eg. servers="127.0.0.1:11211,192.168.0.111:11211"
   -->

  </providers> 
 </cacheProvider>
</configuration>

The following parameters can be specified for initializing the cache provider:

  • socketConnectTimeout – Timeout for socket connection with Memcached servers
  • socketTimeout – Timeout for socket read-write operations
  • defaultExpireTime – Default expire time in milliseconds

To use the Cache Provider in a project, add a reference to CacheProvider.dll and access the methods using the DistCache static class. When creating keys for storing data, avoid using spaces in keys, e.g., "My Key". This is an issue with the C# Memcached client. To close the provider gracefully, add a call to DistCache.Shutdown() in Global.asax file's Application_End event.

Update Dec 24, 2007: To report any issues and enhancements, please go to this link. I have started working on the Session State Provider for Memcached. I will be releasing it very soon on CodePlex.com.

Update Dec 31, 2007: I have released the Session State Provider for Memcached. Please go to this link.

References

  1. http://www.danga.com/memcached/
  2. http://jehiah.cz/projects/memcached-win32/
  3. https://sourceforge.net/project/showfiles.php?group_id=152153
  4. Sample Project Provider by Memcached C# client 1.1.5
  5. http://www.infoq.com/news/2007/07/memcached
  6. http://msdn2.microsoft.com/en-US/library/aa479038.aspx
  7. http://www.linuxjournal.com/article/7451

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Web Developer
United States United States
My name is Fahad Azeem. I am interested in distributed software development. Currently I am working for a software consulting company in Chicago which developes software in .NET platform.

My Blog: http://fahadaz.blogspot.com

Comments and Discussions

 
GeneralA Free distributed cache Pin
Wes Malik26-Jan-11 18:06
Wes Malik26-Jan-11 18:06 
GeneralMy vote of 1 Pin
ericliu668813-May-10 7:22
ericliu668813-May-10 7:22 
Generalhttp://sharepointcache.codeplex.com/ Pin
IaxiomI14-Feb-10 5:02
IaxiomI14-Feb-10 5:02 
QuestionHow to get this work with MemcachedProvider 1.2 and ASP.NET 3.5?? Pin
Maxi Ng @ TW21-Oct-09 20:38
Maxi Ng @ TW21-Oct-09 20:38 
GeneralMy vote of 2 Pin
Maxi Ng @ TW21-Oct-09 20:35
Maxi Ng @ TW21-Oct-09 20:35 
Generalwell done your implementation Pin
roni schuetz23-Sep-07 4:13
roni schuetz23-Sep-07 4:13 
QuestionIs the Cache in Asp.net 2.0 implemented with a provider model? Pin
TormodHystad16-Aug-07 4:17
TormodHystad16-Aug-07 4:17 
AnswerRe: Is the Cache in Asp.net 2.0 implemented with a provider model? Pin
l0t3k21-Aug-07 7:11
l0t3k21-Aug-07 7:11 
GeneralRe: Is the Cache in Asp.net 2.0 implemented with a provider model? Pin
Fahad Azeem4-Feb-08 11:11
Fahad Azeem4-Feb-08 11:11 
GeneralNunit CheckExpire_TimeSpan_Positive_Test Error Pin
Vercors8-Aug-07 2:22
Vercors8-Aug-07 2:22 
GeneralRe: Nunit CheckExpire_TimeSpan_Positive_Test Error Pin
zhaodengpan8-Oct-07 23:17
zhaodengpan8-Oct-07 23:17 
GeneralRe: Nunit CheckExpire_TimeSpan_Positive_Test Error Pin
Vercors9-Oct-07 3:35
Vercors9-Oct-07 3:35 
QuestionHow about using webfarm for sessionstate ? Pin
Kent Liu30-Jul-07 16:40
professionalKent Liu30-Jul-07 16:40 
AnswerRe: How about using webfarm for sessionstate ? Pin
Fahad Azeem31-Jul-07 4:10
Fahad Azeem31-Jul-07 4:10 
GeneralRe: How about using webfarm for sessionstate ? Pin
roni schuetz23-Dec-07 15:43
roni schuetz23-Dec-07 15:43 
GeneralRe: How about using webfarm for sessionstate ? Pin
Fahad Azeem23-Dec-07 18:16
Fahad Azeem23-Dec-07 18:16 
GeneralRe: How about using webfarm for sessionstate ? Pin
Fahad Azeem24-Dec-07 4:47
Fahad Azeem24-Dec-07 4:47 
GeneralRe: How about using webfarm for sessionstate ? Pin
NurhakKaya15-Aug-12 3:37
NurhakKaya15-Aug-12 3:37 
GeneralRe: How about using webfarm for sessionstate ? Pin
NurhakKaya15-Aug-12 21:23
NurhakKaya15-Aug-12 21:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.