views:

93

answers:

3

I have an ASP.NET website where i have implemented page level caching using the OutPutCache directive.This boosted the page performance.My pages has few parts(Some buttons,links and labels) which are specific to the logged in user.If user is not logged in,they will see different links.Now Since i implemented the page level caching,Even after the user logged in,It's showing the old page content(Links and buttons meant for the Non logged in User).

Caching is obviously good.But how to get rid of this problem ? Do i need to completely remove caching ?

+2  A: 

You can use the VaryByParam directive:

VaryByParam: This attribute allows us to control how many cached versions of the page should be created based on name/value pairs sent through HTTP POST/GET. The default value is None. None implies that only one version of the page is added to the Cache, and all HTTP GET/POST parameters are simply ignored. The opposite of the None value is *. The asterisk implies that all name/value pairs passed in are to be used to create cached versions of the page. The granularity can be controlled, however, by naming parameters (multiple parameter names are separated using semi-colons).

Used like so in the page directive

<%@ OutputCache Duration="10800" VaryByParam="State;City" %>

Be careful what you use in the VaryByParam, as this can cause the number of copies of the page in memory to be up to the number of different values of your parameter that exist.

EDIT: as mentioned in comments, this won't work if you're using cookies for login, but some people do use cookie-less login, which puts the info in the GET/POST portion.

See here for more details

David
comments are good if you're going to downvote
David
+1, not to balance the downvote, but because it actually deserves an upvote. VaryByParam would be what I would first look at to solve the problem, and it's limitations is also mentioned in the answer.
Guffa
Didn't downvote, but VaryByParam is limited to GET/POST values. If the user is authenticated by a cookie or session value VaryByParam wouldn't work.
Chris Pebble
I would also have to point out since the OP mentions different per user VaryByParam would be pointless because this param would be Per User which would make caching have no value, unless the difference is only per logged in vs not logged in.
Chris Marisic
+2  A: 

I ran into the exact same issue and was able to resolve it using Response.WriteSubstitution. Just create a static method that accepts HttpContext as an argument, returns the login status as a string, and render the method using WriteSubstitution:

Response.WriteSubstitution(new HttpResponseSubstitutionCallback(GetLoginStatus));

The rest of the page will cache as normal but the login status will be updated each time the page is loaded.

Chris Pebble
+3  A: 

what you want is Partial Page caching: http://msdn.microsoft.com/en-us/library/ms227429.aspx and http://msdn.microsoft.com/en-us/library/h30h475z.aspx

Tim Mahy