Vary by custom

R

Ryan Moore

I am trying to "tune" some user controls in ASP.NET for optimal caching.

Is it possible to do a vary by param - on a Session Variable, so the page
caches when a Session variable stays the same, but refreshes when the
session changes?

thnx
 
H

Hermit Dave

What you could potentially do is set objects from within the ascx user
control, directly into the cache and set a manual file based dependancy.
all you need to to reset the cache is modify the file. (create an empty
file, set it as being a dependancy.. when you want to reset the cache.. just
change the time stamp)

Copying some text from msdn for user control caching
VaryByCustom
Any text that represents custom output caching requirements. If this
attribute is given a value of browser, the cache is varied by browser name
and major version information. If a custom string is entered, you must
override the HttpApplication.GetVaryByCustomString method in your
application's Global.asax file.
VaryByHeader
A semicolon-separated list of HTTP headers used to vary the output cache.
When this attribute is set to multiple headers, the output cache contains a
different version of the requested document for each specified header.
Note Setting the VaryByHeader attribute enables caching items in all
HTTP 1.1 caches, not just the ASP.NET cache. This attribute is not supported
for @ OutputCache directives in user controls.
VaryByParam
A semicolon-separated list of strings used to vary the output cache. By
default, these strings correspond to a query string value sent with GET
method attributes, or a parameter sent using the POST method. When this
attribute is set to multiple parameters, the output cache contains a
different version of the requested document for each specified parameter.
Possible values include none, *, and any valid query string or POST
parameter name.
CAUTION This attribute is required when you output cache ASP.NET pages.
It is required for user controls as well unless you have included a
VaryByControl attribute in the control's @ OutputCache directive. A parser
error occurs if you fail to include it. If you do not want to specify a
parameter to vary cached content, set the value to none. If you want to vary
the output cache by all parameter values, set the attribute to *.
VaryByControl
A semicolon-separated list of strings used to vary a user control's output
cache. These strings represent the ID property values of ASP.NET server
controls declared in the user control. For more information, see Caching
Portions of an ASP.NET Page.
 
S

Scott Allen

Ryan:

Your subject line just about gives you the answer. Use VaryByCustom in
the @OutputCache directive.

<%@ OutputCache Duration="30" VaryByParam="none"
VaryByCustom="MySpecialSessionVariable" %>

Then in global.asax, you need to overide GetVaryByCustomString:

public override string GetVaryByCustomString(
HttpContext context,
string arg)
{
string result = String.Empty;

if(arg == "MySpecialSessionVariable")
{
object o = Session["MySpecialSessionVariable"];
if(o != null)
{
result = o.ToString();
}
}
else
{
result = base.GetVaryByCustomString(context, arg);
}

return result;
}
 
R

Ryan Moore

OK, Got that to work, now it seems that I'm having problems when I have
multiple controls loaded into a page, each with a seperate
"customcachestring".... is there anything I should know about this? when I
have 2 seperate user controls which use custom strings, only one of the
controls shows up on the page!

thnx


Scott Allen said:
Ryan:

Your subject line just about gives you the answer. Use VaryByCustom in
the @OutputCache directive.

<%@ OutputCache Duration="30" VaryByParam="none"
VaryByCustom="MySpecialSessionVariable" %>

Then in global.asax, you need to overide GetVaryByCustomString:

public override string GetVaryByCustomString(
HttpContext context,
string arg)
{
string result = String.Empty;

if(arg == "MySpecialSessionVariable")
{
object o = Session["MySpecialSessionVariable"];
if(o != null)
{
result = o.ToString();
}
}
else
{
result = base.GetVaryByCustomString(context, arg);
}

return result;
}



I am trying to "tune" some user controls in ASP.NET for optimal caching.

Is it possible to do a vary by param - on a Session Variable, so the page
caches when a Session variable stays the same, but refreshes when the
session changes?

thnx
 
Joined
Aug 24, 2006
Messages
1
Reaction score
0
That would be wonderfull if it worked. However - at this point the session has not yet been created. I'm hoping you have some magical way of forcing the session to be created at this point.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top