Memcached (分布式缓存) ASP.NET Provider






3.54/5 (13投票s)
用于在 Web 场中使用 Memcached 的提供程序
引言
目前,我正在开发一个将在Web服务器集群中托管的基于Web的项目。 在几个问题中,我们遇到了分布式缓存的问题。 众所周知,ASP.NET中当前的缓存是进程内缓存,无法在Web服务器集群中使用。 在做了一些研究后,我们在网上找到了一些解决方案,但最终,所有这些方案都存在可伸缩性问题。 我们找到了一些第三方实现,但它们价格昂贵。 然后我遇到了由Danga Interactive实现的Memcached。 这是一个高性能的分布式内存缓存,最初是为http://www.LiveJournal.com实现的。 原始实现运行在 *nix 系统上。 幸运的是,对于那些希望在 Windows 环境中运行它的人,可以在http://jehiah.cz/projects/memcached-win32/找到 Win32 端口。 有关 Memcached 工作的更多详细信息,请阅读这篇文章。
有一个适用于 Memcached 的 C# 客户端,可以从这里下载。 为了编写此提供程序,我使用了 clientlib 1.1.5。
Using the Code
以下是缓存提供程序的接口
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();
}
以下是如何在 web.config 或 app.config 文件中配置缓存提供程序
<?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>
可以指定以下参数来初始化缓存提供程序
- socketConnectTimeout– 与 Memcached 服务器进行套接字连接的超时时间
- socketTimeout– 套接字读写操作的超时时间
- defaultExpireTime– 默认过期时间(毫秒)
要在项目中使用缓存提供程序,请添加对 CacheProvider.dll 的引用,并使用 DistCache static 类访问方法。 在为存储数据创建键时,避免在键中使用空格,例如 "My Key"。 这是一个关于 C# Memcached 客户端的问题。 为了优雅地关闭提供程序,请在 Global.asax 文件的 Application_End 事件中添加对 DistCache.Shutdown() 的调用。
更新于 2007年12月24日:要报告任何问题和增强功能,请访问此链接。 我已经开始开发 Memcached 的会话状态提供程序。 我很快会在 CodePlex.com 上发布它。
更新于 2007年12月31日:我已发布了 Memcached 的会话状态提供程序。 请访问此链接。
参考文献
- http://www.danga.com/memcached/
- http://jehiah.cz/projects/memcached-win32/
- https://sourceforge.net/project/showfiles.php?group_id=152153
- Memcached C# 客户端 1.1.5 提供的示例项目提供程序
- http://www.infoq.com/news/2007/07/memcached
- http://msdn2.microsoft.com/en-US/library/aa479038.aspx
- https://linuxjournal.cn/article/7451


