Page Cache
Use 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 meaning 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. Example above
, Varybyparam is set to requestid. Therefore, ASP. NET uses different values of the requestid parameter.
In the get query string, or in the HTTP
Post parameters. You can check the value of the requestid parameter to make the applicationProgramDifferentiate users by placing the outputcache command on the page
Varybyparam = "requestid" allows ASP. NET to cache different 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 the cached language to the ascx file.
Instead of writing cache statements in the aspx file, ASP. NET can cache only the output of ascx fragments. Generally, it is the same as the header or footer, and does not need to be reloaded. However
If there is dynamic data in it, it cannot be cached, because once it is cached, the program will not create its instance to update the data display, and only wait until the lifetime expires, so this is not the case.
It is 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, which is located in ASP. NET
The system. Web. caching namespace can be used to cache and generate resource-consuming data. Usage of the cache class and session and Application Object 1
Simple. 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 make things easier, the page class
The cache attribute enables the application's cache object instanceCode.
Data cached through the cache object is stored in the application memory. This means that the lifetime of the data is not
The restart of the application exceeds (in fact, this is the same as the data stored in the application and Session object, unless stateservice or SQL
State session mode stores session data ).
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 item remains in the cache until an event occurs.
Meaning of correlation value 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.
Meaning of priority value 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
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
Use timespan. Zero. The sixth parameter uses a value in the system. Web. caching. cacheitempriority enumeration to set the priority
Set to normal.
Specifies a 5-minute elastic expiration time. No absolute expiration time is specified.
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, the 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 a notification when a cache item is deleted from the cache. You can compile a custom method (like the itemremovedcallback () method here ), 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 ),
Cacheitempriority. 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 itself, and the third parameter is the reason why the cached items are deleted.