alexmen

专注.net软件开发,项目管理体系PMBOK.

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
using System;

using System.Collections.Generic;

using System.Web;

using System.Configuration;

using System.Collections;

/// <summary>

///CacheHelper 的摘要说明

/// </summary>

public class CacheHelper

{

    #region 从配置文件中读取缓存时间

    //缓存时间

    private static string _CacheTime = string.Empty;

    public static string CacheTime

    {

        get

        {

            try

            {

                _CacheTime = ConfigurationManager.AppSettings["CacheTime"].ToString();

            }

            catch (Exception)

            {

                _CacheTime = "0";

            }

            return _CacheTime;

        }

        set { _CacheTime = value; }

    }

    #endregion

    public CacheHelper()

    {

        //

        //TODO: 在此处添加构造函数逻辑

        //

    }

    #region 插入Cache

    /// <summary>

    /// 插入Cache

    /// </summary>

    /// <typeparam name="T"></typeparam>

    /// <param name="o"></param>

    /// <param name="key"></param>

    public static void Add<T>(T o, string key)

    {

        HttpContext.Current.Cache.Insert(

            key,

            o,

            null,

            DateTime.Now.AddMinutes(Convert.ToDouble(CacheTime)),  // Cache的缓存时间,通常建议配置在Config文件中

            System.Web.Caching.Cache.NoSlidingExpiration);

    }

    #endregion

    #region 删除指定的Cache

    /// <summary>

    /// 删除指定的Cache

    /// </summary>

    /// <param name="key">Cache的key</param>

    public static void Clear(string key)

    {

        HttpContext.Current.Cache.Remove(key);

    }

    #endregion

    #region 判断Cache是否存在

    /// <summary>

    /// 判断Cache是否存在

    /// </summary>

    /// <param name="key"></param>

    /// <returns></returns>

    public static bool Exists(string key)

    {

        return HttpContext.Current.Cache[key] != null;

    }

    #endregion

    #region 取得Cache值,带类型 T

    /// <summary>

    /// 取得Cache值,带类型 T

    /// </summary>

    /// <typeparam name="T"></typeparam>

    /// <param name="key"></param>

    /// <param name="value"></param>

    /// <returns></returns>

    public static bool Get<T>(string key, out T value)

    {

        try

        {

            if (!Exists(key))

            {

                value = default(T); //

                return false;

            }

            value = (T)HttpContext.Current.Cache[key];

        }

        catch

        {

            value = default(T);

            return false;

        }

        return true;

    }

    #endregion

    #region 清除所有缓存

    /// <summary>

    /// 有时可能需要立即更新,这里就必须手工清除一下Cache

    ///Cache类有一个Remove方法,但该方法需要提供一个CacheKey,但整个网站的CacheKey我们是无法得知的

    ///只能经过遍历

    /// </summary>

    public static void RemoveAllCache()

    {

        System.Web.Caching.Cache _cache = HttpRuntime.Cache;

        IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();

        ArrayList al = new ArrayList();

        while (CacheEnum.MoveNext())

        {

            al.Add(CacheEnum.Key);

        }

        foreach (string key in al)

        {

            _cache.Remove(key);

        }

    }

    #endregion

    #region 显示所有缓存

    //显示所有缓存

    public static string show()

    {

        string str = "";

        IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();

        while (CacheEnum.MoveNext())

        {

            str += "<br />缓存名<b>[" + CacheEnum.Key + "]</b><br />";

        }

        return "当前网站总缓存数:" + HttpRuntime.Cache.Count + "<br />" + str;

    }

    #endregion

}

 

posted on 2014-07-30 22:31  alexmen  阅读(973)  评论(0)    收藏  举报