Darrell Norton's Blog [MVP]

Sponsors

The Lounge

Wicked Cool Jobs

News

  • Darrell Norton pic

    MVP logo

    View Darrell Norton's profile on LinkedIn

    Currently Reading:

    weewar.com

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
ASP.NET "VaryByCustom" page output caching

Steve Eichert was interested in how to create a custom caching setup, so here are some notes I took from the Devscovery conference.

We all know that the OutputCache directive allows ASP.NET to cache fully-rendered pages. We can instruct ASP.NET to cache different copies based on form variables like this:
<%@ OutputCache Duration=”60” VaryByParam=”ZipCode” %>

Or we can cache based on HTTP Headers like this:
<%@ OutputCache Duration=”60” VaryByParam=”None” VaryByHeader=”User-Agent” %>

The key to creating a custom cache variance is understanding that ASP.NET uses a simple string comparison to determine if a cached result should be returned instead of processing the page. For example, say we want to cache a certain page by SessionID. We add the OutputCache directive like this:
<%@ OutputCache Duration=”60” VaryByParam=”None” VaryByCustom=”SessionID” %>

Now, in global.asax, we must override the GetVaryByCustomString method, like this:

Public override string GetVaryByCustomString(HttpContext context, string arg)
{
  if(arg.ToLower() == “sessionid”)
  {
    HttpCookie cookie = context.Request.Cookies[“ASP.NET_SessionID”];
    if(cookie != null)
      return cookie.Value;
  }
  return base.GetVaryByCustomString(context, arg);
}

That’s it. Simple, elegant, beautiful.


Posted Tue, May 4 2004 5:40 AM by Darrell Norton

[Advertisement]

Comments

Steve wrote RE: ASP.NET "VaryByCustom" page output caching
on Tue, May 4 2004 4:00 AM
Nice! Thanks for posting it!
Darrell wrote re: ASP.NET "VaryByCustom" page output caching
on Tue, May 4 2004 5:59 AM
Steve - my pleasure. I haven't done any ASP.NET stuff lately even though that is one of the things I know most about.
Francis Shanahan wrote re: ASP.NET "VaryByCustom" page output caching
on Wed, Jan 12 2005 8:29 AM
How would this work with authentication? If I'm doing forms authentication and want to vary by whether or not the user's logged in etc, do I just check for an Identity and return a string "true", "false"?
Darrell wrote re: ASP.NET "VaryByCustom" page output caching
on Wed, Jan 12 2005 9:35 AM
If you only wanted to vary based on whether someone was logged in (not who was logged in), then yes. That's all you would have to do.
Francis Shanahan wrote re: ASP.NET "VaryByCustom" page output caching
on Wed, Jan 12 2005 10:06 AM
Well, I actually want to verify the role of the authenticated user aswell but I figured I'd keep the question simple.

Thanks, I'll try this tonight.
Francis Shanahan wrote re: ASP.NET "VaryByCustom" page output caching
on Thu, Jan 13 2005 9:08 AM
Doesn't seem to work. It caches but just for a few mins. then seems to flush the cache and regen the page. My directive is set to duration of 50000 my GetVaryByCustomString simply returns "fred" ALWAYS (I was trying to force it to always cache in an effort to debug). Any ideas? What could be happening?

See the behaviour here: The middle portion is cached, the time of caching is printed in tiny text at the top of the middle section. This time matches the server time (top RIGHT) when the page is generated and then should be less than that time in the cached version.
'ppreciate the help.

http://www.francisshanahan.com/default.aspx
Brian Pedersen wrote re: ASP.NET "VaryByCustom" page output caching
on Tue, Mar 8 2005 6:09 AM
Have you noticed, that in some browsers, or in some combinations of browser/personal firewall, you get a new session id each time you refresh (F5) the browser?

Apparently, the session is (sometimes) only stored if the session instance is actually used.

To solve this problem, add the following line to your code-behind of your .aspx file:

this.Session["junk"] = System.DateTime.Now;

Actually, you can add anything to any session name. What matters is that you use the session instance of the page.
Darrell wrote re: ASP.NET "VaryByCustom" page output caching
on Tue, Mar 8 2005 8:09 AM
Brian - good catch. I always put a little something in Session state so I had not noticed. Thanks!
DotNetKicks.com wrote ASP.NET
on Sat, May 24 2008 9:09 AM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Devlicio.us