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

39 篇文章 1 订阅

博客园已经全面使用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,理解还不深,这篇随笔写得也很简单,只是希望能抛个砖,希望园子里有更多朋友分享与探讨在缓存方面的心得。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值