• HttpRuntime.Cache在ASP.NET的使用


    一.缓存:

    • 5个等级的缓存
      1. 1级是网络级缓存,缓存在浏览器,CDN以及代理服务器中   (举个例子:每个帮助页面都进行了缓存,访问一个页面的代码非常简单)
      2. 2级是由.net框架 HttpRuntime.Cache完成,在每台服务器的内存中。
      3. 3级Redis,分布式内存键值存储,在多个支撑同一个站点的服务器上共享缓存项。
      4. 4级SQL Server Cache,整个数据库,所有数据都被放到内存中。
      5. 5级SSD。通常只在SQL Server预热后才生效

    二.具体使用

            添加cachehelper类

     /// <summary>
        /// 缓存帮助类
        /// 时间:2016/05/07
        /// </summary>
        public class CacheHelper
        {
            #region 获取数据缓存
    
            /// <summary>
            /// 获取数据缓存
            /// </summary>
            /// <param name="cacheKey"></param>
            /// <param name="objObject"></param>
            public static object GetCache(string cacheKey)
            {
                var objCache = HttpRuntime.Cache;
                return objCache[cacheKey];
            }
            #endregion
    
            #region 设置数据缓存
            /// <summary>
            /// 设置数据缓存
            /// </summary>
            public static void SetCache(string cacheKey, object objObject)
            {
                var objCache = HttpRuntime.Cache;
                objCache.Insert(cacheKey, objObject);
            }
    
            /// <summary>
            /// 设置数据缓存
            /// </summary>
            public static void SetCache(string cacheKey, object objObject, TimeSpan timeout)
            {
                var objCache = HttpRuntime.Cache;
                objCache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null);
            }
    
            /// <summary>
            /// 设置数据缓存
            /// </summary>
            public static void SetCache(string cacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
            {
                var objCache = HttpRuntime.Cache;
                objCache.Insert(cacheKey, objObject, null, absoluteExpiration, slidingExpiration);
            }
            #endregion
    
            #region 移除缓存
            /// <summary>
            /// 移除指定数据缓存
            /// </summary>
            public static void RemoveAllCache(string cacheKey)
            {
                var cache = HttpRuntime.Cache;
                cache.Remove(cacheKey);
            }
    
            /// <summary>
            /// 移除全部缓存
            /// </summary>
            public static void RemoveAllCache()
            {
                var cache = HttpRuntime.Cache;
                IDictionaryEnumerator cacheEnum = cache.GetEnumerator();
                while (cacheEnum.MoveNext())
                {
                    cache.Remove(cacheEnum.Key.ToString());
                }
            }
            #endregion      
        }
    View Code

           添加完对此进行调用先判断是否已经cache没有则添加

    var cacheKey = "sid" + assessId + "uid" + studentId + "pindex" + pageHelper.PageIndex;
                var cachelist = CacheHelper.GetCache(cacheKey) as List<Question>;
                if (cachelist != null)
                {
                    return cachelist;
                }
    获取数据
    
        var result = GetAssessQuseList(pageHelper, tableName, fields, null, orderBy, true, studentId, otherFields);
                CacheHelper.SetCache(cacheKey, result, TimeSpan.FromSeconds(120));
                return result;
    View Code

      下面是移除cache

     var cacheKey = "sid" + assessId + "uid" + base.LoggingUser.UserId + "pindex" + pageIndex;
                var cachelist = CacheHelper.GetCache(cacheKey) as List<Question>;
                if (cachelist != null)
                {
                    CacheHelper.RemoveAllCache(cacheKey);
    
                }
    View Code
  • 相关阅读:
    iOS中静态库的制作——使用创建静态库项目的方式创建静态库
    在Xcode中自定义代码块——快速代码
    在博客园的第一篇博客
    委派
    As I Began to Love Myself
    When you are old
    RESOURCE SUMMARY
    First One
    js 动态生成div显示id
    js 层的显示和隐藏
  • 原文地址:https://www.cnblogs.com/songStar/p/8493938.html
Copyright © 2020-2023  润新知