星空网 > 软件开发 > ASP.net

好用的Cache辅助工具类

欢迎各位大神提建议,目前的需求,这个工具类已经够用了。好用的Cache辅助工具类好用的Cache辅助工具类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;using System.Web;using System.Web.Caching;namespace Tools{  /// <summary>  /// 缓存辅助类  /// </summary>  public static class CacheHelper  {    /// <summary>    /// 缓存    /// </summary>    private static Cache cache = HttpRuntime.Cache;    /// <summary>    /// 根据键获取缓存数据    /// </summary>    /// <param name="cacheKey">缓存的键</param>    /// <returns></returns>    private static object GetCache(string cacheKey)    {      return cache.Get(cacheKey);    }    /// <summary>    /// 设置缓存    /// </summary>    /// <param name="cacheKey">缓存的键</param>    /// <param name="objValue">缓存的值</param>    private static void SetCache(string cacheKey, object objValue)    {      cache.Insert(cacheKey, objValue);    }    /// <summary>    /// 设置缓存    /// </summary>    /// <param name="cacheKey">缓存的键</param>    /// <param name="objValue">缓存的值</param>    /// <param name="slidingExpiration">滑动过期时间</param>    private static void SetCache(string cacheKey, object objValue, TimeSpan slidingExpiration)    {      cache.Insert(cacheKey, objValue, null, Cache.NoAbsoluteExpiration, slidingExpiration);    }    /// <summary>    /// 设置缓存    /// </summary>    /// <param name="cacheKey">缓存的键</param>    /// <param name="objValue">缓存的值</param>    /// <param name="absoluteExpiration">绝对过期时间</param>    /// <param name="slidingExpiration">滑动过期时间</param>    private static void SetCache(string cacheKey, object objValue, DateTime absoluteExpiration)    {      cache.Insert(cacheKey, objValue, null, absoluteExpiration, Cache.NoSlidingExpiration);    }    /// <summary>    /// 移除指定的缓存    /// </summary>    /// <param name="cacheKey">缓存的键</param>    public static void Remove(string cacheKey)    {      cache.Remove(cacheKey);    }    /// <summary>    /// 移除全部缓存    /// </summary>    public static void Remove()    {      IDictionaryEnumerator CacheEnum = cache.GetEnumerator();      while (CacheEnum.MoveNext())      {        cache.Remove(CacheEnum.Key.ToString());      }    }    /// <summary>    /// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。    /// </summary>    /// <typeparam name="T">缓存的数据类型</typeparam>    /// <param name="cacheKey">缓存的键</param>    /// <param name="getData">回调方法</param>    /// <returns>缓存中的数据</returns>    public static T Get<T>(string cacheKey, Func<T> getData)    {      var data = GetCache(cacheKey);      if (data == null)      {        data = getData();        SetCache(cacheKey, data);      }      return (T)data;    }        /// <summary>    /// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。    /// </summary>    /// <typeparam name="T">缓存的数据类型</typeparam>    /// <param name="cacheKey">缓存的键</param>    /// <param name="slidingExpiration">滑动过期时间</param>    /// <param name="getData">回调方法</param>    /// <returns>缓存中的数据</returns>    public static T Get<T>(string cacheKey, TimeSpan slidingExpiration, Func<T> getData)    {      var data = GetCache(cacheKey);      if (data == null)      {        data = getData();        SetCache(cacheKey, data, slidingExpiration);      }      return (T)data;    }    /// <summary>    /// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。    /// </summary>    /// <typeparam name="T">缓存的数据类型</typeparam>    /// <param name="cacheKey">缓存的键</param>    /// <param name="absoluteExpiration">绝对过期时间</param>    /// <param name="getData">回调方法</param>    /// <returns>缓存中的数据</returns>    public static T Get<T>(string cacheKey, DateTime absoluteExpiration, Func<T> getData)    {      var data = GetCache(cacheKey);      if (data == null)      {        data = getData();        SetCache(cacheKey, data, absoluteExpiration);      }      return (T)data;    }  }}

View Code

 





原标题:好用的Cache辅助工具类

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

拉塔基亚海运:https://www.goluckyvip.com/tag/99319.html
入驻Joom:https://www.goluckyvip.com/tag/9932.html
直布罗陀海运:https://www.goluckyvip.com/tag/99320.html
南非拼箱海运:https://www.goluckyvip.com/tag/99321.html
纽约海运直达:https://www.goluckyvip.com/tag/99322.html
沙特海运物流:https://www.goluckyvip.com/tag/99323.html
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流