日期:2014-05-17  浏览次数:20464 次

【散300分】公布网站上的一个缓存类,希望大家多指教
一开始我的网站上没有用缓存,很明细速度非常慢。用过缓存之后速度明显好多了。

大家看看这几个方法,当初也是在网上学习之后的笔记,然后整理出来的一个类。

大家看看有什么不足,或需要改进的地方。 我的网站:我爱编码网

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 
    /// &