Asp.net uses Cache caches that reside on the page to regularly update data.

Source: Internet
Author: User

It is to keep the Web Application running from the beginning to the end. Some people say why not use the Application? In fact, the Cache can automatically update data within a period of time, and the Application cannot do this. In addition, the Application must consider thread security issues in a high-concurrency Web system, the Application itself is not thread-safe, and the Cache is thread-safe. Therefore, in most objects, I obtain data only once from the database or file when the Web starts to run. Cache is used on different pages, in addition, the data in the Cache may be automatically updated. Therefore, you do not need to consider data updates. In addition, the Cache is also associated with applications, all pages that may reside in the Web application.

First of all, we should familiarize ourselves with System. web. caching. cache class. Here I will only talk about several methods and attributes that are used in the class. For more details, refer to MSDN.

First, we will introduce its Add () method to Add the specified object to the Cache object set.

The Insert () method overwrites the Cache top with the same Key.

Remove () removes a specified item from the application's Cache object.

The Count attribute to obtain the number of objects stored in the cache.

Here I mainly want to talk about the Add () method, because if we want to have a Cache for a long time and automatically replace it within a period of time, we must know it very well. Let's take a look at the detailed introduction to this method in MSDN. Oh.

C #

public Object Add(
string key,
Object value,
CacheDependency dependencies,
DateTime absoluteExpiration,
TimeSpan slidingExpiration,
CacheItemPriority priority,
CacheItemRemovedCallback onRemoveCallback
)
Parameters
Key
Type: System. String

The cache key used to reference this item.

Value
Type: System. Object

The entry to be added to the cache.

Dependencies
Type: System. Web. Caching. CacheDependency

The file dependency or cache key dependency. When any dependency item is changed, the object is invalid and removed from the cache. If there is no dependency, this parameter contains the nullNothingnullptrnull reference (Nothing in Visual Basic ).

AbsoluteExpiration
Type: System. DateTime

The time when the added object expires and is removed from the cache. If you use the callable expiration, the absoluteExpiration parameter must be NoAbsoluteExpiration.

SlidingExpiration
Type: System. TimeSpan

The interval between the last access to the added object and the expiration time of the object. If this value is equivalent to 20 minutes, the object will expire and be removed from the cache 20 minutes after the last access. If you use an absolute expiration date, the slidingExpiration parameter must be NoSlidingExpiration.

Priority
Type: System. Web. Caching. CacheItemPriority

The relative cost of the object, represented by CacheItemPriority enumeration. This value is used when the cache exits the object. Objects with lower costs are removed from the cache before they have higher costs.

OnRemoveCallback
Type: System. Web. Caching. CacheItemRemovedCallback

The delegate called when an object is removed from the cache (if provided ). When you delete an application object from the cache, you can use it to notify the application.

Return Value

Type: System. Object

Remarks

If an item with the same key parameter is saved in the Cache, the call to this method fails. To overwrite existing Cache items with the same key parameter, use the Insert method.

The absoluteExpiration and slidingExpiration parameters cannot be set at the same time. If you want to make the cache entry expire at a specific time, you can set the absoluteExpiration parameter to a specific time and set the slidingExpiration parameter to NoSlidingExpiration.

If you want the cache item to expire after a certain period of time after the last access to the item, you can set the slidingExpiration parameter to the expiration interval and set the absoluteExpiration parameter to NoAbsoluteExpiration.

This section is reproduced from MSDN

How to add the cache? Generally, I will add the cache to the Init event or Load event on the master page, this ensures that the referenced program can cache the objects that need to be frequently used from the beginning.

Protected void Page_Load (object sender, EventArgs e)
{
Cache. Add ("key", // key to be added to the Cache
New {value = "add value"}, // corresponding value
Null, // cache dependency.
DateTime. Now. AddMinutes (1), // fixed Cache Time
System. Web. Caching. Cache. NoSlidingExpiration, // you can set the delay Cache time,
System. Web. Caching. CacheItemPriority. NotRemovable, // cache priority.
New System. Web. Caching. CacheItemRemovedCallback (OnMoveCacheBack); // callback function called upon removal
}

Public void OnMoveCacheBack (string key, object value, System. Web. Caching. CacheItemRemovedReason reason)
{
If (Cache [key]! = Null)
{
Cache. Remove (key );
}
Cache. Add ("key", // key to be added to the Cache
New {value = "Update value"}, // corresponding value
Null, // cache dependency.
DateTime. Now. AddMinutes (1), // fixed Cache Time
System. Web. Caching. Cache. NoSlidingExpiration, // you can set the delay Cache time,
System. Web. Caching. CacheItemPriority. NotRemovable, // cache priority.
New System. Web. Caching. CacheItemRemovedCallback (OnMoveCacheBack); // callback function called upon removal
}

You must pay attention to three points for the specific use of parameters,

First, the cache dependency must be specified as null.

Second, the fixed expiration cache time cannot be set at the same time as the delayed cache time. To change data on a regular basis, you must use the fixed expiration cache time.

The third is the cache priority, which is also critical and must be specified as System. web. caching. cacheItemPriority. notRemovable enumeration value, so that it will not be automatically withdrawn, but be sure to pay attention to the size of the cache.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.