zoukankan      html  css  js  c++  java
  • C# Cache缓存读取设置

    先创建一个CacheHelper.cs类,代码如下

    [csharp] view plain copy
    1. using System;  
    2. using System.Web;  
    3. using System.Collections;  
    4. using System.Web.Caching;  
    5.   
    6. public class CacheHelper  
    7. {  
    8.     /// <summary>  
    9.     /// 获取数据缓存  
    10.     /// </summary>  
    11.     /// <param name="cacheKey">键</param>  
    12.     public static object GetCache(string cacheKey)  
    13.     {  
    14.         var objCache = HttpRuntime.Cache.Get(cacheKey);  
    15.         return objCache;  
    16.     }  
    17.     /// <summary>  
    18.     /// 设置数据缓存  
    19.     /// </summary>  
    20.     public static void SetCache(string cacheKey, object objObject)  
    21.     {  
    22.         var objCache = HttpRuntime.Cache;  
    23.         objCache.Insert(cacheKey, objObject);  
    24.     }  
    25.     /// <summary>  
    26.     /// 设置数据缓存  
    27.     /// </summary>  
    28.     public static void SetCache(string cacheKey, object objObject, int timeout = 7200)  
    29.     {  
    30.         try  
    31.         {  
    32.             if (objObject == null) return;  
    33.             var objCache = HttpRuntime.Cache;  
    34.             //相对过期  
    35.             //objCache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null);  
    36.             //绝对过期时间  
    37.             objCache.Insert(cacheKey, objObject, null, DateTime.Now.AddSeconds(timeout), TimeSpan.Zero, CacheItemPriority.High, null);  
    38.         }  
    39.         catch (Exception)  
    40.         {  
    41.             //throw;  
    42.         }  
    43.     }  
    44.     /// <summary>  
    45.     /// 移除指定数据缓存  
    46.     /// </summary>  
    47.     public static void RemoveAllCache(string cacheKey)  
    48.     {  
    49.         var cache = HttpRuntime.Cache;  
    50.         cache.Remove(cacheKey);  
    51.     }  
    52.     /// <summary>  
    53.     /// 移除全部缓存  
    54.     /// </summary>  
    55.     public static void RemoveAllCache()  
    56.     {  
    57.         var cache = HttpRuntime.Cache;  
    58.         var cacheEnum = cache.GetEnumerator();  
    59.         while (cacheEnum.MoveNext())  
    60.         {  
    61.             cache.Remove(cacheEnum.Key.ToString());  
    62.         }  
    63.     }  
    64. }  


    引用也贴在上面了,就这么几个。

    然后是调用:

     

    [csharp] view plain copy
    1. public IEnumerable<CompanyModel> FindCompanys()  
    2.         {  
    3.             var cache = CacheHelper.GetCache("commonData_Company");//先读取  
    4.             if (cache == null)//如果没有该缓存  
    5.             {  
    6.                 var queryCompany = _base.CompanyModel();//从数据库取出  
    7.                 var enumerable = queryCompany.ToList();  
    8.                 CacheHelper.SetCache("commonData_Company", enumerable);//添加缓存  
    9.                 return enumerable;  
    10.             }  
    11.             var result = (List<CompanyModel>)cache;//有就直接返回该缓存  
    12.             return result;  
    13.         }  


    测试结果也贴上来看看好了:


    首次加载进来是为null,然后读取数据库,添加进缓存,当前返回前台的是从数据库中取出的数据。


    刷新页面,发现缓存中已经有了读出的30条数据,


    然后接下来走,返回缓存中的数据:

    大致这些了。

    End

  • 相关阅读:
    pdf 去水印 比较好用
    ffmpeg从视频中每隔几秒提取一张图片
    tampermonkey 网盘直链下载助手
    淘工厂买的usb 蓝牙5.0 外置台式机 适配器(下载)
    Apache Doris 轻松入门和快速实践
    一步一步编译最新版Apache Doris 0.15版本的详细过程
    go使用msgpack
    服务接口杂谈
    关于真伪3层所讨论的
    jattach最近的一些更新
  • 原文地址:https://www.cnblogs.com/xuhongfei/p/8951878.html
Copyright © 2011-2022 走看看