cache problem

  • Thread starter Thread starter jack
  • Start date Start date
J

jack

Now here i have a doubt
the explorer maintains the cache for 300 seconds when the code is in
html code like the one which is below :-
<%@ OutputCache Duration="300" VaryByParam="none" %>


But the same code when typed in the cs file of the page does not gives
the same result.

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Private);
Im not able to guess as to you the page is not been cached for 300
seconds
 
Hello Jack,
Now here i have a doubt
the explorer maintains the cache for 300 seconds when the code is in
html code like the one which is below :-
<%@ OutputCache Duration="300" VaryByParam="none" %>
But the same code when typed in the cs file of the page does not gives
the same result.

Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
Response.Cache.SetCacheability(HttpCacheability.Private);
Im not able to guess as to you the page is not been cached for 300
seconds

Well, 60 seconds and 300 seconds are not the same ;-)

Your second approach doesn't set the CacheControl: max-age header, either.
To achieve the same as the OutputCache directive in code, use

// You could also use new TimeSpan(0, 0, 300), but 5 minutes is more readable
than 300 seconds :-)
TimeSpan maxAge = new TimeSpan(0, 5, 0);
Response.Cache.SetExpires(DateTime.Now.Add(maxAge));
Response.Cache.SetCacheability(HttpCacheability.Private);
Response.Cache.SetMaxAge(maxAge);

Cheers,
 
Back
Top