.NET PETSHOP Detailed (5): PETSHOP Output Cache Settings

xiaoxiao2021-03-17  178

.NET PETSHOP Detailed (5): PETSHOP Output Cache Settings

ASP.NET output cache

Measuring high performance, the most important indicator of the scalable web application is to cache. ASP.NET provides cache functions for high-performance web applications, and ASP.NET has three caches that can be used by web applications:

• Output cache, which is a dynamic response generated by the cache request.

• The clip cache, which caches the part of the response generated by the request.

· Data cache, which can be programmed to cache any object. To support this cache, ASP.NET provides full-featured cache engines that allow programmers to easily reserve data between requests.

The output cache of the page is very useful. In a massive access site, some page access frequencies account for a very large proportion, even if the output cache is used for these pages, it will also reduce the burden of the system, because the requests for these pages will not be Perform the code created this page.

However, this is not flexible enough, the request may be a lot of requests, but we cach all things on the page, whether it is a part of constructive high or constructing low-cost. Can there be a part of the part of the page? Fortunately, ASP.NET provides sections for each request to create or customize the page. For example, we can make a clip-cache for user controls that construct highly constructed on the page.

The ASP.NET cache supports files and cache-dependents to enable developers to enable cache items to depends on external files or other cache items. This technique can be used to make this item invalid when changing the basic data source of the item. ASP.NET can store these items on a web server or other software in the request stream, such as proxy servers or browsers. This allows you to avoid recreate information that meets the previous request, especially when you create a large number of processor time or other resources when you create it on the server.

PETSHOP page cache settings

We can implement the output cache of the page by using the low-level OutputCache API or advanced @ OutputCache instruction. After enabling the output cache, create an output cache item when you issue the first GET request to the page. Subsequent GET or HEAD requests are served by the output cache item until the cache request expires. The output cache also supports the cache GET or POST name / value to the variant.

Output Cache Follow the Expired and Validity Policy. If a page is in the output cache, and there is an expiration policy tag indicating that the page has expired 60 minutes after 60 minutes, then the page is removed from the output cache after 60 minutes. If another request is received, the page code is executed, and the page can be cached again.

The following instructions activate the output cache when responding:

<% @ Outputcache duration = "60" VarybyParam = "none"%>

Duration and VaryByParam are the necessary parameters, the former identifies the expiration time, the latter represents a string of the GET or POST name / value pair. If this property is not used, it is set to none. Here we also explain a parameter VaryByCustom, use this parameter, we can customize any text of the output cache requirements. In addition to this property in the OutputCache instruction, we have to rewrite the GetVaryByCustomString method to specify the behavior of the output cache for custom strings in the code declaration block of the application's global.asax file.

Lift a column:

<% @ Outputcache varybyparam = "none" varybycustom = "categorypageKey" location = "server" duration = "43200"%>

The VaryByCustom is defined here for categoryPageKey, then in Global.asax we must define the behavior of categorypageKey this character creation cache, see below. Public override string getvarybycustomstring (httpContext context, string arg) {

String cachekey = "";

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 ["tem_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;

}

Return CacheKey;

}

From the above to CategoryPageKey character creation, our request page contains a request to request a request for a paging of a specific category_ID (naturally the page has been cached).

The following table lists the output cache settings for PETSHOP web applications.

ASP.NET WebForms

Cache setting

Duration

ControlHeader

<% @ OutputCache

DURATION = "43200"

Varybyparam = "none"

Varybycustom = "UserID"%>

12 Hours

DEFAULT

<% @ OutputCache

DURATION = "43200"

Varybyparam = "none"

Varybycustom = "UserID"%>

12 Hours

Help

<% @ OutputCache

DURATION = "43200"

Varybyparam = "none"

Varybycustom = "UserID"%>

12 Hours

Category

<% @ OutputCache

DURATION = "43200"

Varybyparam = "none"

Varybycustom = "categorypageKey"%>

12 Hours

PRODUCT

<% @ OutputCache

DURATION = "43200"

Varybyparam = "none"

Varybycustom = "ProductPageKey"%>

12 Hours

ProductDetails

<% @ OutputCache

DURATION = "43200"

Varybyparam = "none"

Varybycustom = "ProductDetailSpageKey"%>

12 Hours

Search

<% @ OutputCache

DURATION = "43200"

Varybyparam = "none"

Varybycustom = "searchPageKey"%> 12 Hours

Obviously, the ControlHeader on the upper part of the Web page of PETSHOP is related to the status of the user login, so it sets the Cache version of the user's different login states. The Category page is very necessary to cache due to a large number of access, and the amount of data is very necessary, but due to the randomness of the data, there are different versions, such as the Category of different categories, or even different pagings display The data page is used here with the VaryByCustom property to cache different versions of the page.

PETSHOP sniffer

In the previous we mentioned the cache of local data on page, usually some consumerous components, such as user controls. In PETSHOP, a large number of user controls (especially .NET sample programs duwamish 7.0 use more user controls, those pages are simply the assembly of controls), the cache setting method of the user control and the cache setting method of the ASPX page basics The same, we are not listed here. Only the CONTROLHEADER control uses a cache setting, see the table

转载请注明原文地址:https://www.9cbs.com/read-129612.html

New Post(0)