Vary by custom

  • Thread starter Thread starter Ryan Moore
  • Start date Start date
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
 
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.
 
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;
}
 
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
 
Back
Top