Page fragment cache

zhaozj2021-02-16  56

In addition to the output cache hie, ASP.NET also provides you with a simple way to output a cached page content area, which is appropriately referred to as a fragment cache. use

User Control Elite the area of ​​the page and use the previous section

@ OutputCache instructions Tag them as a cache. This instruction specifies the duration (in seconds) of the output content of the user control to cache on the server, and alter the options depend on this duration.

For example, the following instructions indicate the ASP.NET output cache user control 120 seconds and use the "CategoryID" and "SelectedID" query strings or form publishing parameters to change the cache.

<% @ Outputcache duration = "120" VarybyParam = "CategoryId; SELECTEDID"%>

The VaryByParam property is very powerful, which allows the user to indicate multiple instances of the ASP.NET to cache / store the output cache area on the server. For example, the following URL cache user control contents of the user control content individually instances.

http://localhost/mypage.aspx? categoryId = foo & selectedId = 0

http://localhost/mypage.aspx? categoryId = foo & selectedId = 1

The logic within the user control can thus dynamically generate different content (separate caches) based on the provided parameters.

In addition to supporting the VaryByParam properties, the clip cache also supports the VaryByControl property. The VaryByParam property is based on the name / value sent by the POST or GET to change the cache result, and the VaryByControl property changes the cache segment through the controls in the user control. E.g:

<% @ Outputcache duration = "120" VarybyParam = "none" VarybyControl = "category"%>

Note: Similar to the output cache page, even if you don't use VarybyParam, it is also required to explicitly use it.

If the user control contains a drop-down selection box control named "category", the output of the user control will change based on the value selected in the control.

As can be recursively recursted in the page (i.e., the user control declared in another server control) can also be recursively selected. This provides a powerful writing model that makes the cached area consists of the area of ​​the further subscript.

The following sample code shows how to use the declarative user control cache page two menu parts.