How to Use the varybycustom attribute of the @ outputcache command to cache pages of different versions (2)

Source: Internet
Author: User

Petshop output cache settings
Petshop
Of Web In the upper part of the page Controlheader It is related to the user's login status, so it is set Varybycustom The cached version that identifies different login statuses of users. While Category Because pages may be accessed in large quantities and the data volume is large, it is necessary to cache them. However, due to the randomness of the data, different versions exist, for example, different types Category And even different data pages are displayed on different pages. Varybycustom Attribute to cache pages of different versions.

Let's look at the specific implementation. Code :
Varybycustom You can customize any text required by the output cache. Besides Outputcache In addition to declaring this attribute in the Command, we haveProgramOf Global. asax In the Code declaration block of the file, rewrite Getvarybycustomstring To specify the output cache behavior for the custom string.

For example:

<% @ Outputcache varybyparam = "NONE" varybycustom = "categorypagekey" location = "server" Duration = "43200" %>

Here Varybycustom Defined Categorypagekey In Global. asax Which must be defined Categorypagekey This character creates the output cache behavior, see the following code.

Public Override StringGetvarybycustomstring (httpcontext context, string Arg ){

StringCachekey = "";

Switch(ARG ){

Case"Categorypagekey ":

If(Request. isauthenticated =True){

Cachekey = "qqq" + context. Request. querystring ["category_id"] + context. Request. querystring ["requestedpage"];

}

Else{

Cachekey = "AAA" + context. Request. querystring ["category_id"] + context. Request. querystring ["requestedpage"];

}

Break;

Case"Searchpagekey ":

If(Request. isauthenticated =True){

Cachekey = "qqq" + context. Request. querystring ["search_text"] + context. Request. querystring ["requestedpage"];

}

Else{

Cachekey = "AAA" + context. Request. querystring ["search_text"] + context. Request. querystring ["requestedpage"];

}

Break;

Case"Productpagekey ":

If(Request. isauthenticated =True){

Cachekey = "qqq" + context. Request. querystring ["name"] + context. Request. querystring ["product_id"] + context. Request. querystring ["requestedpage"];

}

Else{

Cachekey = "AAA" + context. Request. querystring ["name"] + context. Request. querystring ["product_id"] + context. Request. querystring ["requestedpage"];

}

Break;

Case"Productdetailspagekey ":

If(Request. isauthenticated =True){

Cachekey = "qqq" + context. Request. querystring ["item_id"] + context. Request. querystring ["requestedpage"];

}

Else{

Cachekey = "AAA" + context. Request. querystring ["item_id"] + context. Request. querystring ["requestedpage"];

}

Break;

Case"Userid ":

If(Request. isauthenticated =True){

Cachekey = "userid_in ";

}

Else{

Cachekey = "userid_out ";

}

Break;

}

ReturnCachekey;

}

From aboveCategorypagekeyWhen the request page containsWhen you request a data page displayed on a certain page of category_id, the call is cached (the page is cached naturally ).

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.