Cache 缓存操作类

using System.Collections.Generic;
using System.Web;
using System;
namespace DataAccess
{
    /// <summary>
    /// 缓存控制类 
    /// </summary>
    public class CacheControl
    {
        public static List<string> AllUseCacheKey = new List<string>();

        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="absoluteExpiration"></param>
        public static void AddCache(string key, object value, DateTime absoluteExpiration)
        {
            if (!AllUseCacheKey.Contains(key))
            {
                AllUseCacheKey.Add(key);
            }

            HttpContext.Current.Cache.Add(key, value, null, absoluteExpiration, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
        }

   /// <summary>
        /// 取出指定缓存
        /// </summary>
        /// <param name="key"></param>

   public static Object GetCache(string key)
        {
           return HttpContext.Current.Cache[key];
        }
        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="key"></param>
        public static void RemoveCache(string key)
        {
            if (AllUseCacheKey.Contains(key))
            {
                AllUseCacheKey.Remove(key);
            }
            HttpContext.Current.Cache.Remove(key);
        }
        /// <summary>
        /// 清空使用的缓存
        /// </summary>
        public static void ClearCache()
        {
            foreach (string value in AllUseCacheKey)
            {
                HttpContext.Current.Cache.Remove(value);
            }
            AllUseCacheKey.Clear();
        }
    }
}

posted @ 2011-03-18 11:04  如歌  阅读(618)  评论(4)    收藏  举报