The cache mechanism provided by Asp.net has been well implemented. We only need to use it flexibly. this document describes. web. caching. cache, httpcontext. the concepts and applicability of items and output cache are introduced.
System. Web. caching. Cache
Asp.net provides cache objects. Each application domain has a unique cache instance. When an application is started, the cache is created (for example, when IIS is restarted ), cache and application are similar (the APPICATION here is different from httpapplication, the former is unique in appdomain, and the latter is not), and global data can be saved, only the cache provides the special features for Cache Management, dependency and expiration policies.
It mainly includes file dependencies, key-value dependencies, database dependencies, and time-based expiration policies.
The cache exists throughout the application domain. Data in the cache is automatically cleared based on the priority only when the server memory is insufficient. when you restart the application server, the cache is cleared and a new cache is created.
System. web. caching. cache class, you can use system. web. httpruntime. cache or system. web. UI. page and system. web. httpcontext. cache for access, both of which point to the same cache instance. therefore, a unique key must be constructed to resolve the conflict. cache can also be used in non-web applications.
According to the metadata comment: the difference between the two is that system. Web. httpruntime. cache refers to the cache used for the current program.
System. Web. httpcontext. cache is used for the cache of the previous application domain. It has more request context.
(Wondering, it doesn't mean that an application domain has only one cache instance. The httpapplication instance pool shares this cache instance, but what is the cache of the current program, isn't a program running in an application domain ?)
Httpcontext. Items
Httpcontext. items is a similar cache storage method, but in httpcontext. the content cached by items is valid only for the current request. Once the request is completed, the content will be cleared in the new request. its lifecycle is the lifecycle of the page request. used for temporary cache data. currently, more and more websites use user controls, custom controls, or class libraries to build applications. If the data required between them comes from the same data query, you can use httpcontext. items cache.
Httpcontext. the disadvantage of items is that it can only be used in Web-based applications. If you want to use it in the data access layer, so that the control at the presentation layer can be shared to the cache, therefore, only one web layer can be added between the data access layer and the presentation layer.
Output Cache
It can be divided into page output cache and page fragment cache. When the entire page is relatively small, the page cache is used. If only a part of the page changes frequently, use fragment cache.
Page-level cache refers to cache the entire page. It can be implemented through the page outputcache or httpcachepolicy class. You can specify the cache dependency policy, cache storage location, and duration in detail.
Page segment-level cache refers to caching user controls, custom controls, and so on.
For example, set the cache time of the page to 60 and cache the page on the server, varybyparam = "NONE ". the Server caches the data for 60 s. If a new request is received, the cached content is returned. it also applies to the entire application domain, and is likely to be system. web. caching. enhanced version of the cache.
Incorrect. Please also propose created by jecray