Page CacheUse the outputcache command. <% @ Outputcache duration = "3600" location = "any" varybycustom = "Browser" varybyparam = "requestid" %> the duration and varybyparam features are required. Location controls the page cache location
| Location |
Description |
| Any |
Default value. This means that the page output can be cached in the client browser, cached on any "downstream" client (such as a proxy server), or cached on the Web server itself. |
| Client |
Indicates that the output cache can only be stored in the local cache of the client that sends the request (that is, the browser ). |
| Downstream |
Indicates that the output cache can be stored on any device (such as a proxy server) that supports the http1.1 cache. |
| Server |
Indicates that the output cache will be stored on the Web Server |
| None |
Indicates that the output cache is disabled for this page. |
Duration allows us to control the time for the page to survive in the cache (unit: seconds) 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 CacheYou 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 CacheA 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 is used to overwrite 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 item remains in the cache until an event occurs.
| Correlation Value |
Description |
| Cachedependency |
You can specify a file or cache key. If the file changes, the object will be deleted. If the cache key changes, the object is also deleted. |
| Datetime |
This is a datatime value that specifies the cache data expiration time (absolute expiration time) |
| Timespan |
This is a time interval, indicating how long the cache data can be retained in the cache after the last access (elastic expiration time) |
Cacheitempriority is used to specify the priority of the cached data so that the data with low priority can be deleted when the cache is filled up.
| Priority Value |
Description |
| High |
Cache items with this priority cannot be deleted when the memory is insufficient. |
| Abovenormal |
Cache items with this priority are retained first than those with normal or below priority. |
| Normal |
Cache items with this priority are retained first than those with the priority of belownormal and low. |
| Belownormal |
This is the penultimate priority. cache items with this priority are retained first than cache items with the priority set to low. |
| Low |
Cache items with this priority are most likely to be deleted when the memory is insufficient. |
| Default |
The default priority of the cache item is normal. |
| Notremovable |
When a cache item is set to this priority, it is telling ASP. net not to delete it from the cache even if the memory is insufficient. |
Datetime dt = new datetime (datetime. now. year, 12, 31); cache. add ("membersdataset", dsmembers, null, DT, timespan. zero, cacheitempriority. normal, null); the first parameter is the key that references the cached object, and the second parameter is the object to be cached. The third parameter is null (indicating no relevance ). The fourth and fifth parameters are absolute expiration time and flexible expiration time. Here, we specify that the cache should expire on the last day of the current year (DT ). We want to specify an expiration time without elasticity, so the fifth parameter uses timespan. Zero. The sixth parameter uses a value in the system. Web. caching. cacheitempriority enumeration to set the priority to normal. Specifies a 5-minute elastic expiration time, and does not specify an absolute expiration time cache. add ("membersdataset", dsmembers, null, datetime. maxvalue, timespan. fromminutes (5), cacheitempriority. normal, null); add a correlation. In this example, the expiration time also depends on the modification of a file, that is, test. XML file: cachedependency Dep = new cachedependency (@ "C: \ test. XML "); cache. add ("membersdataset", dsmembers, DEP, datetime. maxvalue, timespan. fromminutes (5), cacheitempriority. normal, null); the expiration time depends on another modification in the cache: String [] dependencykeys = new string [1]; dependencykeys [0] = "memberschanged "; cachedependency dependency = new cachedependency (null, dependencykeys); cache. add ("membersdataset", Dsmembers, dependency, datetime. maxvalue, timespan. zero, cacheitempriority. normal, null); the last parameter is of the cacheitemremovedcallback type, which allows us to request notifications when a cache item is deleted from the cache. You can write a custom method (like itemremovedcallback () method), and then specify the method in the 7th parameters: Public void itemremovedcallback (string key, object value, cacheitemremovedreason reason) {} cache. add ("membersdataset", dsmembers, dependency, datetime. maxvalue, timespan. fromminutes (5), cacheitemprior Ity. normal, new cacheitemremovedcallback (this. itemremovedcallback (); the first parameter is the key used to store cached items in the cache, the second parameter is the stored object, and the third parameter is the reason for deleting the cached items.