2010-07-30 219 views
0

我有一个ASPX页面,使我想要缓存的许多数据库查询,以提高应用程序的性能。页面可以是以下三种状态之一:OutputCache动态页面状态

  1. 等待动态数据。
  2. 显示动态数据。
  3. 完成显示动态数据。

动态数据的DateTime存储在使用“Id”GET参数加载的对象中。我想要的行为如下。

a>当页面状态为1时,用户浏览到“MyPage.aspx?Id = x”。在第一次加载时,页面在数据库中查找,检索它期望获取新数据的DateTime,并缓存该页面直到该日期。

b>用户在该对象的DateTime(aka,state = 2)后浏览到“MyPage.aspx?Id = x”。在第一次加载时,由于缓存已过期,页面会动态生成并显示最新的数据库数据。该页面被缓存30秒以提高后续用户的性能。

c>状态更改为3后,用户浏览到“MyPage.aspx?Id = x”。页面现在不会更改,因此没有必要继续查看数据库。缓存设置为在一个月内过期(如果可能,永不过期)。

我试图做到这一点用下面的代码(我的状态被称为“待定”,“INPROGRESS”和“完成”):

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (Request.QueryString["Id"] != null) 
    { 
     int id = Convert.ToInt32(Request.QueryString["Id"]); 
     // load object into var 'm' 

     // set up controls using information from DB (all DB work is abstracted to the class of 'm' 

     if (m.Status == Status.Pending) 
     { 
      Response.Cache.SetExpires(m.Date); 
      Response.Cache.VaryByParams["Id"] = true; 
      Response.Cache.SetCacheability(HttpCacheability.Server); 
      Response.Cache.SetValidUntilExpires(true); 

      // do other stuff related to pending 
     } 
     else if (m.Status == Status.InProgress) 
     { 
      Response.Cache.SetExpires(DateTime.Now.AddSeconds(30)); 
      Response.Cache.VaryByParams["Id"] = true; 
      Response.Cache.SetCacheability(HttpCacheability.Server); 
      Response.Cache.SetValidUntilExpires(true); 

      // do other stuff related to in progress 
     } 
     else 
     { 
      // completed 
      Response.Cache.SetExpires(DateTime.Now.AddMonths(1)); 
      Response.Cache.VaryByParams["Id"] = true; 
      Response.Cache.SetCacheability(HttpCacheability.Server); 
      Response.Cache.SetValidUntilExpires(true); 

      // do other stuff related to completed 
     } 

     // load data into page (uses additional database calls) 
    } 
    else 
     Response.Redirect("~/Default.aspx"); 

我不知道,如果这做什么,我期待它。我用FireBug测试了它,并且Cache-Control头设置为“no-cache”,并且高速缓存的过期日期设置为“Wed Dec 31 1969 18:00:00 GMT-0600(Central Standard Time)” 。当我注释掉上面的Response.Cache行时,缓存控制头设置为“private”,并且缓存的有效日期被设置为与上面相同。

任何想法我在这里做错了或如何更好地测试它?

谢谢!

回答

0

原来缓存正在工作。由于缓存设置为服务器,FireBug没有选择它,所以浏览器不知道内容被缓存。