zoukankan      html  css  js  c++  java
  • [工具类]缓存辅助类

    写在前面

    在项目中的统计模块中,查询耗费的时间,实在是太长了,通过优化sql语句或者添加缓存来提高查询的速度,自己就弄了一个缓存的辅助类,方便操作缓存中的数据。

    CacheHelper

     1 using System;
     2 using System.Collections;
     3 using System.Collections.Generic;
     4 using System.Linq;
     5 using System.Text;
     6 using System.Threading.Tasks;
     7 using System.Web;
     8 using System.Web.Caching;
     9 namespace WebSite.Statistics.Core.Utilities
    10 {
    11     /// <summary>
    12     /// 缓存辅助类
    13     /// </summary>
    14     public static class CacheHelper
    15     {
    16         /// <summary>
    17         /// 根据键获取缓存数据
    18         /// </summary>
    19         /// <param name="cacheKey"></param>
    20         /// <returns></returns>
    21         public static object GetCache(string cacheKey)
    22         {
    23             Cache objCache = HttpRuntime.Cache;
    24             return objCache.Get(cacheKey);
    25         }
    26         /// <summary>
    27         ///  设置缓存
    28         /// </summary>
    29         /// <param name="cacheKey">缓存键</param>
    30         /// <param name="objValue">缓存键</param>
    31         public static void SetCache(string cacheKey, object objValue)
    32         {
    33             Cache cache = HttpRuntime.Cache;
    34             cache.Insert(cacheKey, objValue);
    35         }
    36         /// <summary>
    37         /// 设置缓存
    38         /// </summary>
    39         /// <param name="cacheKey">缓存键</param>
    40         /// <param name="objValue">缓存的值</param>
    41         /// <param name="timeout">过期时间</param>
    42         public static void SetCache(string cacheKey, object objValue, TimeSpan timeout)
    43         {
    44             Cache cache = HttpRuntime.Cache;
    45             cache.Insert(cacheKey, objValue, null, DateTime.MaxValue, timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
    46         }
    47         /// <summary>
    48         /// 设置缓存
    49         /// </summary>
    50         /// <param name="cacheKey">缓存键</param>
    51         /// <param name="objValue">缓存的value</param>
    52         /// <param name="absoluteExpiration">绝对过期时间</param>
    53         /// <param name="slidingExpiration">滑动过期时间</param>
    54         public static void SetCache(string cacheKey, object objValue, DateTime absoluteExpiration, TimeSpan slidingExpiration)
    55         {
    56             Cache cache = HttpRuntime.Cache;
    57             cache.Insert(cacheKey, objValue, null, absoluteExpiration, slidingExpiration);
    58         }
    59         /// <summary>
    60         /// 移除指定的缓存
    61         /// </summary>
    62         /// <param name="cacheKey"></param>
    63         public static void RemoveCache(string cacheKey)
    64         {
    65             System.Web.Caching.Cache cache = HttpRuntime.Cache;
    66             cache.Remove(cacheKey);
    67         }
    68         /// <summary>
    69         /// 移除全部缓存
    70         /// </summary>
    71         public static void RemoveAllCache()
    72         {
    73             System.Web.Caching.Cache cache = HttpRuntime.Cache;
    74             IDictionaryEnumerator CacheEnum = cache.GetEnumerator();
    75             while (CacheEnum.MoveNext())
    76             {
    77                 cache.Remove(CacheEnum.Key.ToString());
    78             }
    79         }
    80     }
    81 }

    总结

    缓存用到的地方很多,封装的这个类,也算很基础的东西了,算是记录一下,以后用到的时候,方便查找。

  • 相关阅读:
    Extjs系列篇(3)—-model数据模型
    js中parseInt()会导致的一些问题
    Extjs系列篇(2)—-初步了解
    一步一步学python(七)
    一步一步学python(六)
    一步一步学python(五) -条件 循环和其他语句
    一步一步学python(四)
    一步一步学python(三)
    MFC socket网络通讯核心代码
    MFC 遍历FTP服务器目录中文乱码问题
  • 原文地址:https://www.cnblogs.com/wolf-sun/p/4469600.html
Copyright © 2011-2022 走看看