CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Darrell Norton's Blog [MVP]

Fill in description here...

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.



Comments

Darrell said:

Steve - my pleasure. I haven't done any ASP.NET stuff lately even though that is one of the things I know most about.
# May 4, 2004 5:59 AM

Francis Shanahan said:

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"?
# January 12, 2005 8:29 AM

Darrell said:

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.
# January 12, 2005 9:35 AM

Francis Shanahan said:

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.
# January 12, 2005 10:06 AM

Francis Shanahan said:

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
# January 13, 2005 9:08 AM

Brian Pedersen said:

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.
# March 8, 2005 6:09 AM

Darrell said:

Brian - good catch. I always put a little something in Session state so I had not noticed. Thanks!
# March 8, 2005 8:09 AM

DotNetKicks.com said:

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

# May 24, 2008 9:09 AM
Check out Devlicio.us!

Our Sponsors

Free Tech Publications