.NET缓存设计的使用方法-创新互联

本篇内容介绍了“.NET缓存设计的使用方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

在巴里坤哈萨克等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、网站制作 网站设计制作按需设计,公司网站建设,企业网站建设,高端网站设计,全网整合营销推广,成都外贸网站制作,巴里坤哈萨克网站建设费用合理。

关于缓存的设计
1、什么情况下用缓存

缓存是提高应用程序性能的好方法之一。运用缓存可以优化数据查询,避免不必要的网络数据回传,和避免执行不必要的完全相同的数据处理逻辑。在实现缓存的时候我们要确定什么时候装入缓存数据。用异步装入缓存或用批处理方式来避免出现客户端数据延迟。
一般来说在一定时间内请求了相同的业务逻辑而没有变更的话,可以采用缓存来设计。数据请求频繁的的请求不适合采用缓存,如论坛的回复,但是论坛的主题是可以采用缓存设计的。


2、缓存设计的步骤
确定缓存数据结构:即设计中哪些数据用到了缓存,设计这些数据的缓存结构
确定缓存什么数据
确定缓存过期规则和清理
确定如何装入缓存数据


3、示例 Community Server的缓存类

复制代码 代码如下:


using System;
  using System.Collections;
  using System.Text.RegularExpressions;
  using System.Web;
  using System.Web.Caching;

  namespace Larry.Cache
  {
      /// <summary>
     /// 缓存类 Community Server的缓存类
     /// </summary>
     public class BaseCache
     {
         /// <summary>
         /// CacheDependency 说明
         /// 如果您向 Cache 中添加某个具有依赖项的项,当依赖项更改时,
         /// 该项将自动从 Cache 中删除。例如,假设您向 Cache 中添加某项,
         /// 并使其依赖于文件名数组。当该数组中的某个文件更改时,
         /// 与该数组关联的项将从缓存中删除。
         /// [C#]
         /// Insert the cache item.
         /// CacheDependency dep = new CacheDependency(fileName, dt);
         /// cache.Insert("key", "value", dep);
         /// </summary>
         public static readonly int DayFactor = ;
         public static readonly int HourFactor = ;
         public static readonly int MinuteFactor = ;
         public static readonly double SecondFactor = 0.;

         private static readonly System.Web.Caching.Cache _cache;

         private static int Factor = ;

         /// <summary>
         /// 单件模式
         /// </summary>
         static BaseCache()
         {
             HttpContext context = HttpContext.Current;
             if (context != null)
             {
                 _cache = context.Cache;
             }
             else
             {
                 _cache = HttpRuntime.Cache;
             }
         }

         /// <summary>
         /// 一次性清除所有缓存
         /// </summary>
         public static void Clear()
         {
             IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
             ArrayList al = new ArrayList();
             while (CacheEnum.MoveNext()) //逐个清除
             {
                 al.Add(CacheEnum.Key);
             }

             foreach (string key in al)
             {
                 _cache.Remove(key);
             }

         }

 

         public static void RemoveByPattern(string pattern)
         {
             IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
             Regex regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
             while (CacheEnum.MoveNext())
             {
                 if (regex.IsMatch(CacheEnum.Key.ToString()))
                     _cache.Remove(CacheEnum.Key.ToString());
             }
         }

         /// <summary>
         /// 清除特定的缓存
         /// </summary>
         /// <param name="key"></param>
         public static void Remove(string key)
         {
             _cache.Remove(key);
         }

         /// <summary>
         /// 缓存OBJECT.
         /// </summary>
         /// <param name="key"></param>
         /// <param name="obj"></param>
         public static void Insert(string key, object obj)
         {
             Insert(key, obj, null, );
         }

        /// <summary>
        /// 缓存obj 并建立依赖项
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <param name="dep"></param>
        public static void Insert(string key, object obj, CacheDependency dep)
        {
            Insert(key, obj, dep, MinuteFactor * );
        }

        /// <summary>
        /// 按秒缓存对象
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <param name="seconds"></param>
        public static void Insert(string key, object obj, int seconds)
        {
            Insert(key, obj, null, seconds);
        }

        /// <summary>
        /// 按秒缓存对象 并存储优先级
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <param name="seconds"></param>
        /// <param name="priority"></param>
        public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
        {
            Insert(key, obj, null, seconds, priority);
        }

        /// <summary>
        /// 按秒缓存对象 并建立依赖项
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <param name="dep"></param>
        /// <param name="seconds"></param>
        public static void Insert(string key, object obj, CacheDependency dep, int seconds)
        {
            Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
        }

        /// <summary>
        /// 按秒缓存对象 并建立具有优先级的依赖项
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <param name="dep"></param>
        /// <param name="seconds"></param>
        /// <param name="priority"></param>
        public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
        {
            if (obj != null)
            {
                _cache.Insert(key, obj, dep, DateTime.Now.AddSeconds(Factor * seconds), TimeSpan.Zero, priority, null);
            }

        }


        public static void MicroInsert(string key, object obj, int secondFactor)
        {
            if (obj != null)
            {
                _cache.Insert(key, obj, null, DateTime.Now.AddSeconds(Factor * secondFactor), TimeSpan.Zero);
            }
        }

        /// <summary>
        /// 较大时间缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        public static void Max(string key, object obj)
        {
            Max(key, obj, null);
        }

        /// <summary>
        /// 具有依赖项的较大时间缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <param name="dep"></param>
        public static void Max(string key, object obj, CacheDependency dep)
        {
            if (obj != null)
            {
                _cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null);
            }
        }

        /// <summary>
        /// Insert an item into the cache for the Maximum allowed time
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        public static void Permanent(string key, object obj)
        {
            Permanent(key, obj, null);
        }

        public static void Permanent(string key, object obj, CacheDependency dep)
        {
            if (obj != null)
            {
                _cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);
            }
        }

        public static object Get(string key)
        {
            return _cache[key];
        }

        /// <summary>
        /// Return int of seconds * SecondFactor
        /// </summary>
        public static int SecondFactorCalculate(int seconds)
        {
            // Insert method below takes integer seconds, so we have to round any fractional values
            return Convert.ToInt(Math.Round((double)seconds * SecondFactor));
        }
    }
}

其实这个类就是一个单件模式的设计 和缓存的公共操作方法,其中CacheDependency表示建立缓存依赖项,CacheItemPriority表示缓存的优先级。S使用如下

复制代码 代码如下:


 public static CardShop.Model.Systems GetConfig()
     {
         const string cacheKey = "WebConfig";
         CardShop.Model.Systems sampleCacheTable = Larry.Cache.BaseCache.Get(cacheKey) as CardShop.Model.Systems;
         if (sampleCacheTable == null)
         {
                                   OprationCheck.Message("第一次加载使用缓存");
             sampleCacheTable = model;
             Larry.Cache.BaseCache.Insert(cacheKey, sampleCacheTable, 24 * Larry.Cache.BaseCache.MinuteFactor);
        }
        else
        {
            OprationCheck.Message("已经加载了缓存不需要再加载");
        }
        return sampleCacheTable;
    }

“.NET缓存设计的使用方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!

网站标题:.NET缓存设计的使用方法-创新互联
文章位置:https://www.cdcxhl.com/article24/ghhce.html

成都网站建设公司_创新互联,为您提供网站制作微信公众号服务器托管营销型网站建设用户体验网站维护

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都定制网站网页设计