NorthScale Memcached Server的.NET客户端NorthScaleClient使用简介

来源:互联网 发布:java aop 日志记录 编辑:程序博客网 时间:2024/05/17 16:44

博客园已经全面使用NorthScale Memcached Server,用下来感觉不错。下面简单分享一下.NET客户端的使用方法。

NorthScale Memcached Server官方推荐的.NET客户端是EnyimMemcached。EnyimMemcached有两种方式可以访问NorthScale Memcached Server:

1) Enyim.Caching.MemcachedClient(Enyim.Caching.dll);

2) NorthScale.Store.NorthScaleClient(Northscale.Store.dll)。NorthScaleClient是基于Enyim.Caching针对NorthScale Memcached Server专门开发的客户端。

这两个客户端的主要区别在于:

1)Enyim.Caching.MemcachedClientt只能访问NorthScale Memcached Server的默认Bucket(默认访问服务器11211端口);

2)NorthScale.Store.NorthScaleClient只能访问NorthScale Memcached Server(默认访问服务器8080端口)。

这里主要讲一下NorthScaleClient的使用方法:

1. 下载NorthScaleClient,下载地址:http://github.com/enyim/EnyimMemcached/downloads

2. 在项目中添加对Northscale.Store.dll的引用。

3. 在web.config中添加相应的配置:

  a) 在configSections中添加下面的配置:

<section name="northscale" type="NorthScale.Store.Configuration.NorthScaleClientSection, NorthScale.Store" />

  b) 在configuration中添加如下的配置:

    <northscale>        <servers>            <add uri="http://localhost:8080/pools/default" />            <add uri="http://localhost:8080/pools/default" />        </servers>        <socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00"/>    </northscale>

  注:如果只有一台Memcached服务器,要在这里填两个相同的uri,不然调用时会报错。

4. 客户端调用示例代码:

    public class EnyimMemcachedProvider : ICacheProvider    {        private static NorthScaleClient client;        static EnyimMemcachedProvider()        {            try            {                client = new NorthScaleClient();            }            catch (Exception ex)            {                log4net.LogManager.GetLogger("cnblogs").Info("EnyimMemcachedProvider", ex);            }                    }        #region ICacheProvider Members        public void Add(string key, object value)        {            if (client != null)            {                client.Store(StoreMode.Set, key, value);            }        }        public void Add(string key, object value, int cacheSecond)        {            if (client != null)            {                client.Store(StoreMode.Set, key, value, new TimeSpan(0, 0, cacheSecond));            }        }        public object GetData(string key)        {            if (client == null)            {                return null;            }            return client.Get(key);        }        public void Remove(string key)        {            if (client != null)            {                client.Remove(key);            }        }        #endregion    }

需要注意的地方:

1) 由于创建一个NorthScaleClient的对象成本很高,所以这里要使用静态变量private static NorthScaleClient client。

2) 在NorthScale Memcached Server停运时,NorthScaleClient会抛出"none of the pool urls are working."的异常,而不是写入日志,这样会影响网站的正常访问。建议在静态构造函数中创建NorthScaleClient对象,在创建对象失败时捕获异常并写入日志。

在高性能.NET Web应用开发中,缓存是很重要的环节,之前.NET平台缺少好的缓存解决方案,而NorthScale Memcached Server+EnyimMemcached客户端让我们眼前一亮。

由于刚接触NorthScale Memcached Server,理解还不深,这篇随笔写得也很简单,只是希望能抛个砖,希望园子里有更多朋友分享与探讨在缓存方面的心得。

原创粉丝点击