Tutorial :Is there conditional caching in ASP.NET?



Question:

Is there a built-in asp.net way to conditionally serve pages, for example I want the following logic:

If there is a session data I generate a page, if there is no session data I serve the cached page.

I am only interested in knowing about a built-in asp.net mechanism for this. If it does not exist I am probably going to simply cache my page manually and decide whether to serve it or not for each request, based on the session data availability.


Solution:1

I don't think there is built-in support (like varyByParam) for generating fresh output for users with Session Data.

As you suggest, I would recommend manually caching the pages. I would probably determine the user's Session state in the PreRequestHandlerExecute event handler in the Global.asax and then maybe set:

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);  


Solution:2

At the risk of karmabombing, I really don't like this approach to caching.

For me if a GET request is made, then a server should respond to that in good faith. Caching at a page level should be controlled by http headers because the primary goal is not to get the redundant request at all - you don't want to allocate server/bandwidth resources full stop.

Caching objects which are resources involved in making up a page I can totally get behind, but I can't see great arguments for caching a page wholesale.

Respect the headers.


Solution:3

You might want to look at the substitution control (http://geekswithblogs.net/ranganh/archive/2005/05/04/39003.aspx) new in .NET 2.0, however it might not be exactly what you are after.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »