ASP. NET Cache)

Source: Internet
Author: User

ASP. NET provides a programming function for caching information in an ASP. NET application. This function is similar to the Application object, but it has the ability to dynamically maintain cache information during the lifecycle of ASP. NET applications. Caching data in applications can greatly improve performance, because the data is local to the server and can be quickly searched. Instead of re-querying the original data source. In particular, the biggest bottleneck of the current system is the database. The rational use of the cache can reduce the access to many unnecessary databases, thus optimizing the system performance.

The Cache object improves the ability to implement Relevance-based expiration and callback notifications. Callback notifications allow other users managing applications or ASP. NET to operate on Cache items. Cache objects can store any type (value type or application type) before updating or deleting them. Cache is an object that is more flexible than the Application object. It supports:

1. expiration and deletion based on Relevance-the Cache object provides the ability to bind automatic expiration and deletion of Cache values to a file event, a specific or relative date/time, or another Cache. key capability.

2. Automatic lock management-Cache objects include internal lock management, which provides synchronization for atomic updates (updates that can be placed in a computing operation.

3. callback mechanism-the Cache object provides the ability to set the callback function to be called to delete Cache items.

You can Add () or Insert () to the Cache. You can use optional parameters for the Insert method. The default parameters are used to Add the Cache:

Cache. Add (KeyName, KeyValue, Dependencies, AbsoluteExpiration, SlidingExpiration, Priority, CacheItemRemoveCallback );

Parameters are: cache name, cached object, dependency, absolute expiration time, relative expiration time, priority, and events caused by cache expiration.

 

Cache dependencies: file dependencies, other cache dependencies, database dependencies, and expiration time methods are set. When the dependencies change, the cache will become invalid and some events will be triggered.

1. File dependency: cache depends on xml files:

Show row number copy code?
  1. DataSet dsProducts = new DataSet ();//... number of dsProducts y Data YCacheDependency fileDepends = new CacheDependency (Server. mapPath ("Northwind. xml "); Cache. insert ("dsProducts", dsProducts, fileDepends );

2. Other cache item dependencies:
Show row number copy code?
  1. object cacheData;string[] fileDependsArray = { Server.MapPath("Northwind.xml") };string[] cacheDependsArray = { "Depend0", "Depend1", "Depend2" };CacheDependency cacheDepends = new CacheDependency(fileDependsArray, cacheDependsArray);Cache.Insert("cacheName", cacheData, cacheDepends);

3. expiration time settings:

AbsoluteExpiration can be used to set the absolute expiration time of the cache, for example, 30 minutes after expiration:

Show row number copy code?
  1. Cache.Insert("cacheName ", "cacheValue", null, DateTime.Now.AddMinutes(30), Cache.NoSlidingExpiration);

NoSlidingExpiration allows you to set the relative expiration time. If the cache is not accessed within the time specified by NoSlidingExpiration, the cache is not accessed within a certain period of time and expires, if you have access within a certain period of time, the cache will not expire. If you access the Cache within 30 minutes, it will not expire:

Show row number copy code?
  1. Cache.Insert("cacheName", "cacheValue", null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(30));

 

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.