I have an ASPX page that makes a lot of database queries that I am trying to cache to improve performance of the application. The page can be one of three states:
- Waiting for dynamic data.
- Displaying dynamic data.
- Done displaying dynamic data.
The DateTime for the dynamic data is stored in the object that you load with the "Id" GET parameter. I want the behavior to be as follows.
a> User browses to "MyPage.aspx?Id=x" when the state of the page is 1. On first load, the page looks in the database, retrieves the DateTime it expects to get new data, and caches the page until that date.
b> User browses to "MyPage.aspx?Id=x" after the DateTime for that object (aka, state = 2). On first load, since the cache has expired, the page is dynamically generated and shows the latest database data. The page is cached for 30 seconds to improve performance for subsequent users.
c> User browses to "MyPage.aspx?Id=x" after the state has changed to 3. The page is now never going to change, so there's no point to keep looking in the database. The cache is set to expire in a month (or never expire, if that's possible).
I've tried to do this with the following code (my states are called "Pending", "InProgress" and "Complete"):
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");
I'm not sure if this does what I expect it to. I've tested it with FireBug and the Cache-Control header is set to "no-cache" and the expires date of the cache is set to "Wed Dec 31 1969 18:00:00 GMT-0600 (Central Standard Time)". When I comment out the Response.Cache lines above, the cache-control header is set to "private" and the expires date of the cache is set to the same as above.
Any ideas what I'm doing wrong here or how to test it better?
Thanks!