设为首页 收藏本站
查看: 1211|回复: 0

[经验分享] asp.net高性能之路:无缝切换HttpRuntime.Cache与Memcached,附代码

[复制链接]

尚未签到

发表于 2015-8-31 11:33:08 | 显示全部楼层 |阅读模式
概述
  之前网站一直使用asp.net自带的cache,也就是HttpRuntime.Cache。这个的优点是进程内cache,效率非常高,同时对于缓存的对象可以直接获得
  引用,并进行修改,不需要再进行清空缓存。但是使用HttpRuntime.Cache,无法进行扩展,也无法使用web园等等。

方案
  之前有看dudu写的关于northscale memcached的文章,觉得很不错,故进行了一下尝试。由于初次使用,出问题的时候要能随时切换回HttpRuntime.Cache,
  故使用了策略模式,实现无缝切换缓存模式的功能。Memcached的封装类请在https://github.com/enyim/EnyimMemcached/downloads进行下载,我使用的是Northscale.Store.2.8

接口


DSC0000.gif DSC0001.gif View Code


using System;
using System.Collections.Generic;
using System.Web;

/// <summary>
/// 缓存策略接口
/// </summary>
public interface ICacheStrategy
{
    void AddObject(string objId, object o);
    void AddObjectWithTimeout(string objId, object o, int timeoutSec);
    void AddObjectWithFileChange(string objId, object o, string file);
    //void AddObjectWithDepend(string objId, object o, string[] dependKey);

    void RemoveObject(string objId);
    object RetrieveObject(string objId);
    int TimeOut { set; get; }
}

  

HttpRuntime.Cache实现类


View Code


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Caching;
/// <summary>
/// 默认的缓存策略,实现了缓存策略接口
/// </summary>
public class DefaultCacheStrategy : ICacheStrategy
{
    private static readonly DefaultCacheStrategy instance = new DefaultCacheStrategy();
    protected static volatile System.Web.Caching.Cache webCache = System.Web.HttpRuntime.Cache;
    protected int _timeOut = 1; //默认缓存一分钟,也可以单独设置对象的超时时间

    /// <summary>
    /// Initializes the <see cref="DefaultCacheStrategy"/> class.
    /// </summary>
    static DefaultCacheStrategy()
    {
        //lock (syncObj)
        //{
        // //System.Web.HttpContext context = System.Web.HttpContext.Current;
        // //if(context != null)
        // // webCache = context.Cache;
        // //else
        // webCache = System.Web.HttpRuntime.Cache;
        //}
    }

    public int TimeOut
    {
        set { _timeOut = value > 0 ? value : 6000; }
        get { return _timeOut > 0 ? _timeOut : 6000; }
    }

    public static System.Web.Caching.Cache GetWebCacheObj
    {
        get { return webCache; }
    }
    public void AddObject(string objId, object o)
    {
        if (objId == null || objId.Length == 0 || o == null)
        {
            return;
        }
        CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
        if (TimeOut == 6000)
        {
            webCache.Insert(objId, o, null, DateTime.MaxValue, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, callBack);
        }
        else
        {
            webCache.Insert(objId, o, null, DateTime.Now.AddMinutes(TimeOut), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack);
        }
    }

    public void AddObjectWithTimeout(string objId, object o, int timeoutSec)
    {
        if (objId == null || objId.Length == 0 || o == null || timeoutSec <= 0)
        {
            return;
        }
        CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
        webCache.Insert(objId, o, null, System.DateTime.Now.AddSeconds(timeoutSec), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack);
    }
    public void AddObjectWithFileChange(string objId, object o, string file)
    {
        if (objId == null || objId.Length == 0 || o == null)
        {
            return;
        }
        CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
        CacheDependency dep = new CacheDependency(file);
        webCache.Insert(objId, o, dep, Cache.NoAbsoluteExpiration, TimeSpan.FromHours(1), System.Web.Caching.CacheItemPriority.High, callBack);
    }

    //public void AddObjectWithDepend(string objId, object o, string[] dependKey)
    //{
    //    if (objId == null || objId.Length == 0 || o == null)
    //    {
    //        return;
    //    }
    //    CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
    //    CacheDependency dep = new CacheDependency(null, dependKey, DateTime.Now);
    //    webCache.Insert(objId, o, dep, System.DateTime.Now.AddMinutes(TimeOut), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack);
    //}

    public void onRemove(string key, object val, CacheItemRemovedReason reason)
    {
        switch (reason)
        {
            case CacheItemRemovedReason.DependencyChanged:
                break;
            case CacheItemRemovedReason.Expired:
                {
                    //CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(this.onRemove);
                    //webCache.Insert(key, val, null, System.DateTime.Now.AddMinutes(TimeOut),
                    // System.Web.Caching.Cache.NoSlidingExpiration,
                    // System.Web.Caching.CacheItemPriority.High,
                    // callBack);
                    break;
                }
            case CacheItemRemovedReason.Removed:
                {
                    break;
                }
            case CacheItemRemovedReason.Underused:
                {
                    break;
                }
            default: break;
        }
        //TODO: write log here
    }

    public void RemoveObject(string objId)
    {
        //objectTable.Remove(objId);
        if (objId == null || objId.Length == 0)
        {
            return;
        }
        webCache.Remove(objId);
    }

    public object RetrieveObject(string objId)
    {
        //return objectTable[objId];

        if (objId == null || objId.Length == 0)
        {
            return null;
        }
        return webCache.Get(objId);
    }
}
  

Memcached 实现类


View Code


using System;
using System.Collections.Generic;
using System.Web;
using NorthScale.Store;
using Enyim.Caching.Memcached;
/// <summary>
/// Summary description for EnyimMemcachedProvider
/// </summary>
public class EnyimMemcachedProvider
{
        private static NorthScaleClient client;
        static EnyimMemcachedProvider()
        {
            try
            {
                client = new NorthScaleClient();
            }
            catch (Exception ex)
            {
                log4net.LogManager.GetLogger("SojumpLog").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
}
  

策略类


View Code


using System;
using System.Collections.Generic;
using System.Web;
using System.Configuration;
/// <summary>
/// The caching manager
/// </summary>
public class CachingManager
{
    private static ICacheStrategy cs;
    private static volatile CachingManager instance = null;
    private static object lockHelper = new object();
    //private static System.Timers.Timer cacheConfigTimer = new System.Timers.Timer(15000);//Interval in ms

   static CachingManager()
    {
        string type = ConfigurationManager.AppSettings["CacheStrategy"];
        if (type == "2")
            cs = new MemCacheStrategy();
        else
            cs = new DefaultCacheStrategy();
        ////Set timer
        //cacheConfigTimer.AutoReset = true;
        //cacheConfigTimer.Enabled = true;
        //cacheConfigTimer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
        //cacheConfigTimer.Start();
    }

    private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        //TODO:
    }

    public static CachingManager GetCachingService()
    {
        if (instance == null)
        {
            lock (lockHelper)
            {
                if (instance == null)
                {
                    instance = new CachingManager();
                }
            }
        }
        return instance;
    }

    public virtual void AddObject(string key, object o)
    {
        if (String.IsNullOrEmpty(key) || o == null) return;
        lock (lockHelper)
        {
            if (cs.TimeOut <= 0) return;
            cs.AddObject(key, o);
        }
    }

    public virtual void AddObject(string key, object o, int timeout)
    {
        if (String.IsNullOrEmpty(key) || o == null) return;
        lock (lockHelper)
        {
            if (cs.TimeOut <= 0) return;
            cs.AddObjectWithTimeout(key, o, timeout);
        }
    }
    public virtual void AddObject(string key, object o, string file)
    {
        if (String.IsNullOrEmpty(key) || o == null) return;
        lock (lockHelper)
        {
            if (cs.TimeOut <= 0) return;
            cs.AddObjectWithFileChange(key, o, file);
        }
    }
    public virtual object RetrieveObject(string objectId)
    {
        return cs.RetrieveObject(objectId);
    }

    public virtual void RemoveObject(string key)
    {
        lock (lockHelper)
        {
            cs.RemoveObject(key);
        }
    }

    public void LoadCacheStrategy(ICacheStrategy ics)
    {
        lock (lockHelper)
        {
            cs = ics;
        }
    }

    //public void LoadDefaultCacheStrategy()
    //{
    //    lock (lockHelper)
    //    {
    //        cs = new DefaultCacheStrategy();
    //    }
    //}
}
  

调用代码
  string key ="test"
  CachingManager cm = new CachingManager();
  cm.AddObject(key,new Object(),60);//缓存对象60秒。
  

不足
  Memcached无法使用CacheDependency,需要自己去进行处理。如你的缓存对象依赖于文件,则在文件修改时要直接清空缓存。
  Memcached也无法清空某一类的缓存对象,有时因为数据库做了修改,你要清空key以activity_开头的一系列对象的话,是做不到的。
  变通的方案是先将key加入到List或Dictionary中,如下代码:


View Code


public void ClearCacheByPattern(string pattern)
{
     if(client!=null)
       return;
         object c = client.Get("globel_cacheitems");
         if (c==null)
           return;
         List<string> cacheitems = (List<string>) c;
             foreach (string cacheitem in cacheitems)
             {
                 if (cacheitem.StartsWith(pattern))
                 {
                     client.Remove(cacheitem);
                 }
             }
  }
  补充:感谢园友YLH对批量删除缓存的回复:
  Memcached为了高性能而设计,功能少了很多。 如果内存不吃紧的话,批量删除缓存项,可以采用设置key分区,为分区加上版本号来解决。
  要批量删除一个分区的缓存,只需要升级一下缓存分区的版本号即可。目前我们在项目中就是采用这种方案。
  这个方案是目前批量删除缓存项的比较完美的方案了,多谢YLH!
  
如有什么问题,请在下面回复,大家一起讨论。
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-106762-1-1.html 上篇帖子: memcached 源码阅读笔记 下篇帖子: Key/Value之王Memcached初探:三、Memcached解决Session的分布式存储场景的应用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表