具有状态的动态页面的OutputCache

时间:2010-07-30 18:31:11

标签: c# asp.net outputcache

我有一个ASPX页面,它会产生很多我试图缓存的数据库查询,以提高应用程序的性能。该页面可以是以下三种状态之一:

  1. 等待动态数据。
  2. 显示动态数据。
  3. 完成显示动态数据。
  4. 动态数据的DateTime存储在使用“Id”GET参数加载的对象中。我希望行为如下。

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

    B个用户在该对象的DateTime之后浏览“MyPage.aspx?Id = x”(aka,state = 2)。在首次加载时,由于缓存已过期,因此将动态生成页面并显示最新的数据库数据。该页面缓存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”,缓存的过期日期设置为与上面相同。

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

    谢谢!

1 个答案:

答案 0 :(得分:0)

原来缓存工作正常。 FireBug没有提起它,因为可缓存性设置为服务器,因此浏览器不知道内容是否已缓存。