For a long time, I wanted to write more information about ASP. NET cache pages so that I could query them more efficiently. I also wanted to write more information about ASP. NET cache pages. Well, let's talk about it in a few minutes. Today, we just have time to piece it together.
Cache page
Use the outputcache command
- <%@OutputCacheDuration="3600"
- Location="Any"
- VaryByCustom="browser"
- VaryByParam="RequestID"%>
The duration and varybyparam features are required.
Varybyparam allows us to cache different versions of the page. In the preceding example, varybyparam is set to requestid, so ASP. net uses different values of the requestid parameter. These values are either passed in the http get query string or in the http post parameter. You can identify different users by checking the value of the requestid parameter. By placing varybyparam = "requestid" in the outputcache command on the page, Asp.. Net caches different versions of pages for each user.
If you do not want to cache the page version based on the parameter value, set varybyparam to none.
You can also require ASP. NET to cache a version of the page for each possible parameter combination. Therefore, you can set varybyparam *.
The varybyheader and varybycustom features are similar to the varybyparam feature in that they allow you to specify when a new Cache version of the page should be created.
Varybyheader allows us to cache the inconsistent version of the page at the end of the HTTP header list separated by semicolons.
When varybycustom is set to browser, different versions can be cached Based on the browser name and main version information. You can also set it as the name of a custom method to implement our own logic and control the cached version.
Segment Cache
You can use the user control to segment pages and write cache statements to the ascx file instead of the aspx file. In this way, ASP. NET can only cache the output of ascx fragments. Generally, it is the same as the header or footer, and does not need to be reloaded. However, dynamic data cannot be cached, because once cached, the program will not create its instance to update the data display, and only wait until the lifetime expires, therefore, in this case, it is not suitable for caching page fragments.
Note:
1. Note that segment cache does not support the location feature. The only valid part of the cached page segment is the Web server. This is because fragment caching is a new feature in ASP. NET, which is not supported by browsers and proxies.
Ii. Fragment cache has another feature that is not available in the page cache-varybycontrol. The varybycontrol feature allows you to specify a string list separated by semicolons, representing the name of the control used in the user control; ASP. net will generate a cache version of the user component for each different combination of values.
Data Cache
A low-level API is a cache class. It is located in the system. Web. caching namespace in ASP. NET and can be used to cache and generate resource-consuming data. The usage of the cache class is as simple as that of the Application object. Each application has only one cache object-this means that the data stored in the cache using the cache object is application-level data. To simplify the process, the cache attribute of the page class enables the cache object instance of the application to be used in the code.
Data cached through the cache object is stored in the application memory. This means that the lifetime of the data will not exceed the restart of the application (in fact, this is the same as the data stored in the application and Session object unless session data is stored in stateservice or SQL state session mode ).
The usage and syntax are the same as session and application. When the conversion is back, pay attention to the forced type conversion of the corresponding type.
This is not the only way to add cache items to the ASP. NET cache. Cache objects have two methods: insert () and add (), which are more flexible. Their usage is similar, but slightly different:
The insert () method overwrites existing cache items in the ASP. NET cache.
The add () method is only used to add new cache items in the ASP. NET cache (if it is used to overwrite existing cache items, it will fail ).
Each method has seven parameters, and the two methods have the same parameters.
When caching an item, you can specify its relevance to tell ASP. Net that the cached page remains in the cache until an event occurs.