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:
public abstract class CacheProvider : ProviderBase
{
public abstract long Count { get;}
public abstract string[] Servers { get;}
public abstract long DefaultExpireTime { get;set;}
public abstract bool Add(string strKey, object objValue);
public abstract bool Add(string strKey, object objValue,bool bDefaultExpire);
public abstract bool Add(string strKey, object objValue, long lNumofMilliSeconds);
public abstract object Get(string strKey);
public abstract bool RemoveAll();
public abstract object Remove(string strKey);
public abstract void Shutdown();
public abstract bool KeyExists(string strKey);
public abstract Hashtable GetStats();
}
Following is how to configure the Cache Provider in a web.config or app.config file:
="1.0"="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
- http://www.danga.com/memcached/
- http://jehiah.cz/projects/memcached-win32/
- https://sourceforge.net/project/showfiles.php?group_id=152153
- Sample Project Provider by Memcached C# client 1.1.5
- http://www.infoq.com/news/2007/07/memcached
- http://msdn2.microsoft.com/en-US/library/aa479038.aspx
- http://www.linuxjournal.com/article/7451