Speeding Up with Controls and Caching?

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.
 
A

Aidy

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.
 
K

Karl Seguin

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
 
A

Aidy

Interesting, I'll need to play with that. I knew about the VaryByParam but
not ByCustom.
 
E

Erik Lautier

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?
 
K

Karl Seguin [MVP]

No, GetVaryByCustomString must be in global.asax (annoyingly so).

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


Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top