|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Services
Chapters
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionCurrently I am working on a web-based project that will be hosted in a web farm. Among the 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 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 memcahced implemented by Danga Interactive. This is a high performance distributed memory cache initially implemented for http://www.LiveJournal.com. The original implementation runs on *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 detail on the working of Memcached, please read the following article http://www.linuxjournal.com/article/7451. There is a C# client for memcached available and can be downloaded from the following location https://sourceforge.net/projects/memcacheddotnet/. For coding this provider, I have used the clientlib 1.1.5. Using the codeFollowing is the interface for the Cached Provider. 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 Cache Provider in a web.config or app.config file <?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>
Following parameters can be specified for initializing the cache provider
To use Cache Provider in a project add a reference to the CacheProvider.dll and access the methods using Update Dec 31, 2007I have released Session State Provider for memcached. Please go to the following link to download
|
||||||||||||||||||||||
|
PermaLink |
Privacy |
Terms
of Use
Last Updated: 31 Dec 2007 Editor: |
Copyright 2007 by Fahad Azeem Everything else Copyright © CodeProject, 1999-2008 Web17 | Advertise on the Code Project |