A good application usually uses cache to process data to reduce the number of accesses to the database or other storage systems. The purpose is to reduce the server load and make the program run more efficiently.
The cache can be Server-Side or Client-Side. First, let's talk about the server cache. When the application reads data, it first checks whether a cache has been established in the memory. If a cache has been set up, it reads data directly from the cache, which is much faster. When data is updated, the cache and database are updated.
ASP. NET provides a powerful and easy-to-use cache mechanism for you to store objects that require a large number of server resources in the memory. Caching these types of resources will greatly improve the application performance. The Cache is implemented by the Cache class, And the Cache instance is dedicated to each application. The cache lifetime depends on the lifetime of the application. After the application is restarted, it is created again.CacheObject.
DesignCacheClass for ease of use. You can place items inCacheAnd then use a simple key/value pair to retrieve these items. For an example of how to perform this operation, see How to: add an item to the cache and how to: retrieve the value of a cache item.CacheClass provides powerful functions, allowing you to customize how to cache items and how long to cache them. For example, in the absence of system memory, the cache automatically removes rarely used or lower-priority items to release the memory. This technology, also known as cleanup, is one of the ways in which the cache ensures that expired data does not use valuable server resources. When cleaning, you can instructCacheGive certain items a higher priority than others. To indicate the importance of an item, you can specify a CacheItemPriority enumeration value when adding an item using the Add or Insert method.
When usingAddOrInsertWhen you add an item to the cache, you can also create an expiration Policy for the item. You can use the DateTime value to specify the exact expiration time (absolute expiration time) of an item to define the lifetime of the item. You can also use the TimeSpan value to specify an elastic expiration time. The elastic expiration time allows you to specify the running time before the item expires Based on the item's last access time. Once an item expires, it is removed from the cache. The behavior of trying to retrieve its value will returnNull(In Visual BasicNothing), Unless the item is added to the cache again.
For loss-prone items stored in the cache (for example, items that regularly refresh data or those that are valid only for a period of time), an expiration Policy is usually set: as long as the data of these items is kept up-to-date, they are kept in the cache. For example, if you are writing an application that obtains data from another website to track the score of a sports match, as long as the score of the game on the source website is not changed, you can cache these scores. In this case, you can set an expiration policy based on the frequency of score updates on other websites. You can write code to determine whether the cache is the latest score. If the score is not the latest, the code can read the score from the source website and cache the new value.
Finally, ASP. NET allows you to define the validity of cache items based on external files, directories (File dependencies), or another cache item (key dependencies. If items with associated dependencies are changed, the cache items are invalid and removed from the cache. You can use this technology to remove items from the cache when their data sources change. For example, if you write an application that processes financial data in an XML file, you can insert data into the cache from this file and keep a dependency on this XML file. When the file is updated, this item is removed from the cache, your application reads the XML file again, and then refresh the data into the cache.
Note: The Cache object does not have any information about its contained items. It only retains references to these objects. It also provides methods to track their dependencies and set expiration policies.