Caching (partial) pages based on custom strings

Source: Internet
Author: User
Cache | page | string

For example, the site has a Header.ascx user control, each page calls this control as "page Head", can directly cache this user control can submit performance, however, Now this Header.ascx user control will display the login name of the login user, so you cannot use the following caching method directly:

<% @ OutputCache Duration = "86400" VaryByParam = "None"%>
This is the time to use the "cache page according to custom string" method, as follows:

<%@ OutputCache duration= "86400" varybyparam= "None" varybycustom= "Headerpagekey"%>
Then rewrite the method in the global class GetVaryByCustomString

        public override string GetVaryByCustomString (HttpContext context, String custom)
        {
             if (custom== "Headerpagekey")
             {
               // Caching Header.ascx Controls
                if (context. request.isauthenticated)
                 {
                     return context. User.Identity.Name;
               }
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp;     Else
                 {
                     return "Noauthenticated_user";
               }
           }
            return base. GetVaryByCustomString (context, custom);
       }
If the user does not log in, then cache a version, if the login, then the different cache according to the name of the other version, Haha, for each user has cached a version, this should be said is not a good solution.

The above caching technology can refer to http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpguide/html/ Cpconcachingversionsofpagebasedonparameters.asp

In addition, this caching method is used in MSPetShop3.0, which caches different versions depending on the request.querystring and whether it is logged on, but the cache does not seem to be turned on by default.







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.