CacheItemRemovedCallback onRemove = null; //CacheItemRemovedCallback对象
            onRemove = new CacheItemRemovedCallback(ItemRemovedFromCache);
            HttpRuntime.Cache.Insert(
                CacheName,
                Count,
                null,
               DateTime.Now.AddSeconds(10),
               TimeSpan.Zero,
              System.Web.Caching.CacheItemPriority.High,
                onRemove
                );缓存设置这样是 DateTime.Now.AddSeconds(10) 
那么应该是10秒过期 
没错吧?
可是程序运行就是从来不会没过期
调试的时候查看absoluteExpiration=9999/12/31 23:59:59  Orz 请问该怎么样设置缓存过期呢?

解决方案 »

  1.   

    缓存设置没问题,参数上有问题,TimeSpan.Zero改成System.Web.Caching.Cache.NoSlidingExpiration
      

  2.   

    没有问题:
    测试代码:
    Cache1.aspx<%@ Page Language="C#" %><%@ Import Namespace="System.Web.Caching" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">  protected void Page_Load(object sender, EventArgs e)
      {
        CacheItemRemovedCallback onRemove = null; //CacheItemRemovedCallback对象
        onRemove = new CacheItemRemovedCallback(RemovedCallback);
        HttpRuntime.Cache.Insert(
          "mxh",
          "测试",
          null,
          DateTime.Now.AddSeconds(10),
          TimeSpan.Zero,
          System.Web.Caching.CacheItemPriority.High,
          onRemove
          );    Response.Redirect("Cache2.aspx");
      }  public void RemovedCallback(String k, Object v, CacheItemRemovedReason r)
      {
        System.IO.StreamWriter sr = new System.IO.StreamWriter(Server.MapPath("~") + "/test.txt");
        sr.WriteLine(System.DateTime.Now.ToString());
        sr.Close();
      }</script><html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
      <title></title>
    </head>
    <body>
      <form id="form1" runat="server">
      <div>
      </div>
      </form>
    </body>
    </html>
    Cache2.aspx
    <%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">  protected void Page_Load(object sender, EventArgs e)
      {
        if (Cache["mxh"] == null)
        {
          Response.Write("过期");
        }
        else
        {
          Response.Write("没有过期");
        }
      }
    </script><html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
        </div>
        </form>
    </body>
    </html>
    测试方法:打开Cache1.aspx
    过10秒刷新Cache2.aspx并查看根目录下的test.txt
      

  3.   

    具体没弄懂你向测试干什么?不妨看下这个,估计对你有用
     http://support.microsoft.com/kb/323290/zh-cn