日期:2014-05-18  浏览次数:20444 次

ASP.NET 缓存有效时间设置
实现:从添加缓存时间开始,5秒钟后清除该缓存或缓存失效

示例:
C# code

    //循环添加数据到cache
    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 1; i < 5; i++)
        {
            SetValues(i.ToString(), Convert.ToString(i + 1)); //循环添加 4行数据
        }
    }

    //使用Cache记录数据
    public void SetValues(string val1, string val2)
    {
        if (HttpContext.Current.Cache["test"] != null) //缓存存在
        {
            DataTable dtLis = (DataTable)HttpContext.Current.Cache["test"];
            DataRow dr1 = dtLis.NewRow();
            dr1[0] = val1;
            dr1[1] = val2;
            dtLis.Rows.Add(dr1);

            HttpContext.Current.Cache["test"] = dtLis; //使用cache保存记录
        }
        else
        {
            DataTable dtLis = new DataTable();
            dtLis.Columns.Add("Company", typeof(string));
            dtLis.Columns.Add("Department", typeof(string));
            DataRow dr1 = dtLis.NewRow();
            dr1[0] = val1;
            dr1[1] = val2;
            dtLis.Rows.Add(dr1);

            //添加缓存,并且5秒后过期
            Cache.Insert("test", dtLis, null, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration);
            dtLis.Clear();

        }
    }

    //获取缓存
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (HttpContext.Current.Cache["test"] != null)
        {
            DataTable dt = (DataTable)HttpContext.Current.Cache["test"];
            Response.Write(dt.Rows.Count.ToString());
        }
        else
            Response.Write("this cache is null");
    }



以上是我的实现代码,问题是缓存总是存在,隔了5秒后,再获取还是原来的值,并不是null。

问:如何清除掉它啊,设置它5秒后过期。


------解决方案--------------------
抢沙发。。。。。顶缓存知识
------解决方案--------------------
思考中..............
------解决方案--------------------
非常想学习

------解决方案--------------------
DateTime.Now.AddMinutes(5)


你这是五分钟。。。不是五秒
------解决方案--------------------
C# code

if (HttpContext.Current.Cache["test"] != null) //缓存存在
        {
            DataTable dtLis = (DataTable)HttpContext.Current.Cache["test"];
            DataRow dr1 = dtLis.NewRow();
            dr1[0] = val1;
            dr1[1] = val2;
            dtLis.Rows.Add(dr1);
            

            //如果缓存存在 直接取缓存中的数据  为什么要再一次的给缓存赋值呢??不懂。。。
 
            HttpContext.Current.Cache["test"] = dtLis; //使用cache保存记录
        }

------解决方案--------------------
如果缓存存在 ,你再一次的给Cache["test"]赋值, 缓存中的值是你后来给的最新的值 而你创建缓存时候给的值 就被换掉了,导致你设置缓存失效时间 无效。
------解决方案--------------------
C# code

using System;
using System.Web.Caching;
using System.Web;
using System.Collections;
using System.Text.RegularExpressions;


/// <summary>
    /// 缓存常用操作
    /// LastEditTime:2011-10-10
    /// </summary>
    public class CacheUtil
    {
        public CacheUtil()
        {
            //...在此处添加构造函数逻辑
        }

        /// <summary>
        /// 增加一个缓存对象
        /// </summary>
        /// <param name="strKey">键值名称</param>
        /// <param name="valueObj">被缓存对象</param>
        /// <param name="durationMin">缓存失效时间,默认为3分钟</param>
        /// <param name="priority">保留优先级(枚举数值),1最不会被清除,6最容易被内存管理清除,0为default
        /// 【1:NotRemovable;2:High;3:AboveNormal;4:Normal;5:BelowNormal;6:Low】</param>
        /// <returns>缓存写入是否成功true 、 false</returns>
        public static bool InsertCach(s