Using output cache in Asp.net is very simple. You only need to declare on the page or custom control to control the Cache Policy of the page or control.
For example:
1 <% @ outputcache duration = "10" varybyparam = "*" %>
In this way, the cache policy for a page is declared. The cache duration is 10 seconds. Any parameter passed to the page through get or post will invalidate the page output cache.
Detailed descriptions of the outputcache command parameters are provided on msdn.
Note the following when using the output cache:
After an Asp.net page is generated in the cache and becomes invalid, all requests to the page are directly retrieved from the memory and displayed without the page lifecycle of Asp.net. That is to say, the code in various events within the page declaration cycle will not be executed.
The page cannot be cached as a whole, and some departments need to update the page in time. This requires a partial page cache or a cache to be replaced.
Partial page Cache
The custom control declares cache. When this control is called on the page, the control is cached and the rest are not cached.
The cached control does not experience the lifecycle and cannot interact with the page.
Cache replacement
Msdn provides three post-cache replacement solutions:
1. Use the substitution control to implement cache replacement
<Asp: substitution id = "substitution1" runat = "server" methodname = "getdate"/>
C # code:
Protected static string getdate (httpcontext context)
{
Return "<B>" + datetime. Now + "</B> ";
}
Note: (1) the method specified by the methodname attribute of the substitution control must be a static method with parameters of the httpcontext type.
(2) In the getdate method in the preceding example, page attributes such as request, session, and cookies in the method are unavailable because the page lifecycle has not been experienced.
2. response. writesubstitution Method
Response. writesubstitution (New httpresponsesubstitutioncallback (getdate ));
The getdate method is the same as the preceding method.
3. adrotator Control
The adrotator Server Control internally supports cache replacement.
In addition, I found a more alternative cache replacement method, which is suitable for lazy people. That is, use IFRAME to place dynamically updated content in the IFRAME memory.
If the page needs to be cached in a large area, but only one small part cannot be cached, and the dynamic update of this part needs to depend on the page attribute (such as session) or the control value, you can consider IFRAME.
Finally, this problem is found in use. Asp.net caches two versions: one is the version cached when it is first opened, and the other is the first PostBack version after a page is created.
That is to say, after the page is cached, the first PostBack will execute the lifecycle of the page. For future requests, if it is the first request, the cached version that is opened for the first time will be sent. If it is PostBack, the cached version of the first PostBack will be displayed. Why? Why does the first PostBack still work after the page is cached? You can also click here.