##Location
| Meaning
|
##Any
means the page. The output can be cached in the client browser, cached in any "downstream" client (such as a proxy server), or cached in the web server itself |
|
Client
Specifies that the output cache can only be stored in the local cache of the requesting client (i.e. the browser) |
|
Downstream
Specifies that the output cache can be stored in any device that supports HTTP1.1 caching (such as a proxy server) |
|
Server
Specifies the output cache Will be stored on the web server |
|
None
Indicates that output caching is disabled for this page |
|
Duration allows us to control how long the page lives in the cache (in seconds)
VaryByParam allows us to cache different versions of the page. In the above example, VaryByParam is set to RequestID, so ASP.NET uses different values of the RequestID parameter. These values are either passed in the query string of HTTP GET, or It is passed in the parameters of HTTP POST. You can let the application distinguish between different users by checking the value of the RequestID parameter; by placing VaryByParam="RequestID" in the OutputCache directive of the page, you can let ASP.NET cache different versions of the page for each user.
If you don't want to cache the independent version of the page based on the value of the parameter, then just set VaryByParam to none.
You can also ask ASP.NET to cache a version of the page for each possible parameter array combination. To do this, set VaryByParam to *.
The VaryByHeader and VaryByCustom attributes are similar to VaryByParam in that they allow specifying when a new cached version of a page should be created.
VaryByHeader allows us to cache non-directed versions of a page based on a semicolon-separated list of HTTP headers.
VaryByCustom, when set to browser, allows us to cache different versions based on the browser's name and major version information. We can also set it to the name of a custom method to implement our own logic and control the cached version.
Fragment caching
You can use usercontrol to segment the page and write cached statements in the ascx file instead of writing cached statements in the aspx file , so that ASP.NET can only cache the output of the ascx fragment. Generally, if the header or footer is basically the same, there is no need to reload. However, if there is dynamically changing data, it cannot be cached, because once it is cached, the program will not create an instance of it to update the data display. It can only wait until the lifetime expires, so for This situation is not suitable for page fragment caching.
Note:
1. Note that fragment caching does not support the Location feature; the only legal place to cache page fragments is the web server. This is because fragment caching is a new feature in ASP.NET, so browsers and proxy servers do not support it.
2. Fragment cache has another feature that is not found in page cache - VaryByControl. The VaryByControl attribute allows you to specify a semicolon-delimited list of strings that represent the names of controls used within a user control; ASP.NET will generate a cached version of the user widget for each different combination of values.
Data Cache
Low-level API is the Cache class, which is located in the System.web.Caching namespace in ASP.NET, You can use it to cache resource-intensive data. The use of the Cache class is as simple as Session and Applicationobject. There is only one Cache object per application - this means that the data stored in the cache using the Cache object is application-level data. To make things even simpler, the Page class's Cache property makes the application's Cache object instance available in the code. Data cached through the Cache object is stored in the application's memory. This means that the lifetime of this data will not exceed the restart of the application (in fact, this is the same as the data stored in the Application and Session objects, unless StateService or SQL State session mode is used to store Session data).
The specific usage and syntax are the same as Session and Application. When converting back, you need to pay attention to the mandatory
type conversion of the corresponding type.
This is not the only way to add cache items in the ASP.NET cache. The Cache object has two methods, the Insert() method and the Add() method, which are more flexible. Their usage is similar, but slightly different:
The Insert() method is used to overwrite existing cache entries in the ASP.NET cache.
The Add() method is only used to add new cache items to the ASP.NET cache (if you use it to overwrite existing cache items, it will fail).
Each method has 7 parameters, and the parameters of the two methods are the same.
When caching an item, you can specify its relevance and tell ASP.NET that the cached item will remain in the cache until a certain
event occurs.
Relevance value
|
Meaning
|
##CacheDependency
| Allows specifying a file or cache key. If the file changes, the object is deleted. If the cache key changes, the object is also deleted.
|
DateTime
| This is a DataTime value, indicating the cached data expiration time (absolute expiration time)
|
TimeSpan
| This is a time interval that indicates how long cached data can remain in the cache after the last access (elastic expiration time)
|
Use CacheItemPriority to specify the priority of cached data so that low-priority data is deleted when the cache is filled.
Priority value
| Meaning
|
High
| Cache items set to this priority are the least likely to be deleted when out of memory
|
AboveNormal
| Cache items set to this priority will be retained more favorably than cache items with a priority of Normal or below
|
Normal
| Set this priority Cache items with priority levels of BelowNormal and Low have priority to be retained.
|
BelowNormal
| This is the penultimate level of priority level; cache items set to this priority will only be retained in preference to cache items set to Low.
|
Low
| Set to Cache entries of this priority are the
|
Default
| The default value for the priority of cache entries that are most likely to be deleted when out of memory Is Normal
|
##NotRemovable
When the cache item is set to this priority, it is telling ASP.NET not to cache items even if there is insufficient memory. Delete it from cache |
|
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 refers to the cache object, and the second parameter is the object to be cached. The third parameter is null (indicating no correlation).
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 a non-flexible expiration time, so use TimeSpan.Zero for the fifth parameter. The sixth parameter uses a value from the System.Web.Caching.CacheItemPriority enumeration to set the priority to Normal.
Specify a flexible expiration time of 5 minutes, 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, 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 the modification of another item 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 type CacheItemRemovedCallback, allowing us to request notification when a cache item is deleted from the cache , you can write a custom method (like the ItemRemovedCallback() method here), and then specify the method in the 7th parameter:
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 when storing the cache item in the cache, the second is the stored object itself, and the third is the reason for the cache item removal.