NorthScale MemBase Server 1.6 的使用

前几天听到Dudu介绍NorthScale Memcached Server, 感觉很不错,从配置到监控都很方便,于是准备尝试一下。当时在我的Win7 64x安装比较顺利,没有碰到问题,但是安装在一台Win2008 32x的服务器上是总是无法启动,日志提示:

Erlang machine stopped instantly (distribution name conflict?). The service is not restarted, ignoring OnFail option.

从提示表面是服务获知准确的错误信息,经过多次测试发现,是在同一块网卡上绑定多个IP时就会发生这个错误,向NorthScale的开发人员反应问题并希望得到解决,他们给了个重新设置服务IP的方法,如下:

1. from "INSTALLDIR\Memcached Server\bin"
2. run "service_stop.bat"
3. run "service_unregister.bat"
4. run "service_register.bat ns_1@<PHYSICAL IP>"
5. run "service_start.bat"

但是我测试了几次,对多IP没有效果,仍然无法启动服务。

如果服务安装在单IP网卡,以前是正常启动的,在IP发生改变时可以用这个方法修改服务绑定的IP。

一直在等待他们的NorthScale MemBase Server 这个产品的和NorthScale Memcahced Server区别是,MemBase Server的数据会dump到地盘,可以做分布式数据库使用。

之前看姜敏的NorthScale Memcached Server尝试总结中熊哥提到无法使用stats cachedump命令,我看了一下,可能是NorthScale使用Memcached的二进制协议通讯,二进制协议比文本协议高效,但是不支持stats cachedump命令。

前天看到NorthScale MateBase Server 1.6 Beta3发布了,拿来安装测试,发现当初说在此版本要解决的网卡多IP问题还没有解决,期待正式版。

 

现在需要在生产环境中使用NorthScale,但是他不支持stats cachedump,无法获取缓存中的缓存项的key,只好想了一个折中的办法,通过一个缓存来保存目前缓存列表,在每次set时,更新缓存列表。

 

以下是几段代码:

    public class MemCachedCache:ICacheProvider
    {
        private static NorthScaleClient client;

        static MemCachedCache()
        {
            try
            {
                client = new NorthScaleClient();
            }
            catch (Exception ex)
            {
                log4net.LogManager.GetLogger("northscale").Info("EnyimMemcachedProvider", ex);
            }            
        }

        #region Implementation of ICacheProvider

        /// <summary>
        /// 获得缓存内容
        /// </summary>
        /// <param name="cacheKey">缓存键</param>
        /// <returns>缓存内容</returns>
        public string GetCache(string cacheKey)
        {
            if (client == null)
            {
                return null;
            }
            else
            {
                object v = client.Get(cacheKey);
                return v == null ? null:v.ToString();
            }
        }

        /// <summary>
        /// 设置缓存内容
        /// </summary>
        /// <param name="cacheKey">缓存键</param>
        /// <param name="cacheContent">缓存内容</param>
        /// <param name="duration">缓存有效时间,分钟</param>
        public void SetCache(string cacheKey, string cacheContent, int duration)
        {
            if (client != null)
            {
                client.Store(StoreMode.Set, cacheKey, cacheContent, new TimeSpan(0, 0, duration*60));
                UpdateCacheItems(cacheKey);
            }
        }

        /// <summary>
        /// 清除缓存内容
        /// </summary>
        /// <param name="cacheKey">缓存键</param>
        public void ClearCache(string cacheKey)
        {
            if (client != null)
            {
                client.Remove(cacheKey);
            }
        }

        /// <summary>
        /// 清除部分缓存
        /// </summary>
	/// <param name="pattern">部分清除模板</param>
        public void ClearCacheByPattern(string pattern)
        {
            if(client!=null)
            {
                object c = client.Get("globel_cacheitems");
                if (c!=null)
                {
                    List<string> cacheitems = (List<string>) c;
                    foreach (string cacheitem in cacheitems)
                    {
                        if (cacheitem.StartsWith(pattern))
                        {
                            client.Remove(cacheitem);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 清除所有缓存
        /// </summary>
        public void ClearAllCache()
        {
            client.FlushAll();
        }

        /// <summary>
        /// 更新缓存项列边
        /// </summary>
        /// <param name="key">要填加的key</param>
        public void UpdateCacheItems(string key)
        {
            List<string> cacheitems;
            CasResult<object> c = client.GetWithCas("globel_cacheitems");
            //判断列表项缓存是否存在
            if(c.Cas>0)
            {
                cacheitems = (List<string>) c.Result;
                //判断列表中有没有这个key
                if (!cacheitems.Contains(key))
                {
                    cacheitems.Add(key);
                    //判断是否保存成功,这里递归操作并使用Cas,是为了避免并发状态下造成缓存列表竞争,保证最终更新成功
                    if (!client.Cas(StoreMode.Set, "globel_cacheitems", cacheitems, c.Cas).Result)
                    {
                        UpdateCacheItems(key);
                    }
                }
            }
            else
            {
                cacheitems =new List<string>();
                cacheitems.Add(key);
                client.Store(StoreMode.Set, "globel_cacheitems", cacheitems);
            }
        }
        #endregion
    }
posted @ 2010-09-11 23:44  Flymouse  阅读(1392)  评论(2编辑  收藏  举报