For example, the website has a Header.ascx user control. Each page calls this control as the "page header". This user control can be cached directly to submit performance. However, now this Header.ascx user control needs to display the login user's name. Login name, so you cannot directly use the following caching method:
<% @ OutputCache Duration = " 86400 " VaryByParam = " None " %>
At this time, you can use the method of "caching pages based on custom strings", as follows:
<%@ OutputCache Duration="86400" VaryByParam="None" VaryByCustom="HeaderPageKey" %>
Then override the method GetVaryByCustomString in the Global class
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if(custom=="HeaderPageKey")
{
//Cache Header.ascx control
if(context.Request.IsAuthenticated)
{
return context.User.Identity.Name;
}
else
{
return "NoAuthenticated_User";
}
}
return base.GetVaryByCustomString (context, custom);
}
If the user is not logged in, then one version will be cached. If the user is already logged in, then another version will be cached based on the login name. Haha, one version is cached for each user. This is not a good solution.
For the above caching technology, please refer to http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpguide/html/cpconcachingversionsofpagebasedonparameters.asp
In addition, this caching method is also used in MSPetShop3.0 , different versions are cached based on Request.Querystring and whether you are logged in. However, this cache does not seem to be enabled by default.
Source: kaka.net