Speeding Up with Controls and Caching?

  • Thread starter Thread starter Erik Lautier
  • Start date Start date
E

Erik Lautier

I have two subroutines that are running on virtually every page of my
site; it's an if/then/else situation where one runs if the user is
registered and the other runs if the user is a guest. I have two
questions:

1. Since they're on virtually every page, I know that registering them
in a .ascx control will simplify things, but will it actually speed my
pages up?

2. If I go ahead with #1, can I cache the control page and get the
speed boost I'm looking for?

Any caveats about this? Thanks a lot.
 
1. Since they're on virtually every page, I know that registering them
in a .ascx control will simplify things, but will it actually speed my
pages up?

I wouldn't think so, it just makes it easier to manage.
2. If I go ahead with #1, can I cache the control page and get the
speed boost I'm looking for?

Caching can help but remember that you cache the *output* of the control.
Output caching only works when the resulting HTML never changes. If you
control prints "Welcome back <username>" if you are logged in and "Click
here to sign in" if you are not then such a control is not suitable for
output caching.
 
that's not true. Outputcaching allows for variances.

In your case you would use the VaryByCustom property of the OutputCache
control...it's quite simple.

<%@ OutputCache duration="600" VaryByCustom="LoggedIn" VaryByParam="None" %>

and in your global.asax, you'd do:

public override string GetVaryByCustomString(HttpContext context, string
customString)
{
switch(customString)
{
case " LoggedIn":
return (CurrentUserIsLoggedIn) ? "logged" : "notlogged"
break;
}
base.GetVaryByCustomString(context, customString);
}


Karl
 
Thanks to both of you; the only thing is that I have a remote host and
have never messed with global.asax - there are a numbere of files that
they administer, and I assume this is one of them. Would I just put
the below code in every .aspx page then?
 
No, GetVaryByCustomString must be in global.asax (annoyingly so).

You would place the OutputCache tag in your .ascx which displays your two
cases.


Karl
 
Back
Top