你的位置:首页 > ASP.net教程

[ASP.net教程]好用的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