Six Methods for adding ASP. NET cached data. (The difference between insert and add)

Source: Internet
Author: User

Overview of ASP. NET cache data adding requirements

ASP. NET uses the cache mechanism to store objects that require a large amount of server resources in the memory. Caching these types of resources will greatly improve the application performance. Cache is implemented by the cache class. You can set the cache priority cacheitempriority to control the "clean" priority when the memory is insufficient. You can also set an expiration Policy for the cache and a dependency for the cache.

Add ASP. NET cache data (add data items to cache)

1. Add a key-Value Pair

  1. Cache ["cacheitem"] = "cached item ";

2. Use the insert method to add

If the insert method adds an item to the cache and an item with the same name as the existing item already exists, the existing item in the cache will be replaced.

  1. Cache. insert ("cacheitem", "cached item ");

3. Specify and add dependencies (specify dependencies for data items added to the buffer)

When a data item depends on a string array object:

  1. String[] Dependencies = {"dependences "};
  2. Cache. insert ("cacheitem ",
  3. "Cached item ",
  4. NewSystem. Web. caching. cachedependency (Null, Dependencies ));

When a data item depends on an XML file:

  1. Cache. insert ("cacheitem", "cached item ",
  2. NewSystem. Web. caching. cachedependency (
  3. Server. mappath ("xmlfile. xml ")));

Where data items depend on multiple dependencies:

  1. System. Web. caching. cachedependency dep1 =
  2. NewSystem. Web. caching. cachedependency (server. mappath ("xmlfile. xml "));
  3. String[] Keydependencies2 = {"cacheitem1 "};
  4. System. Web. caching. cachedependency dep2 =
  5. NewSystem. Web. caching. cachedependency (Null, Keydependencies2 );
  6. System. Web. caching. aggregatecachedependency aggdep =
  7. NewSystem. Web. caching. aggregatecachedependency ();
  8. Aggdep. Add (dep1 );
  9. Aggdep. Add (dep2 );
  10. Cache. insert ("cacheitem", "cached item", aggdep );

4. Set an expiration Policy and add

Add an absolute expiration time of one minute to the cache:

  1. Cache. insert ("cacheitem", "cached item ",
  2. Null, Datetime. Now. addminutes (1D ),
  3. System. Web. caching. cache. noslidingexpiration );

Add a 10-minute elastic expiration time to the cache:

  1. Cache. insert ("cacheitem", "cached item ",
  2. Null, System. Web. caching. cache. noabsoluteexpiration,
  3. NewTimespan (0, 10, 0 ));

5. set priority and add

Call the insert method to specify a value from the cacheitempriority enumeration.

  1. Cache. insert ("cacheitem", "cached item ",
  2. Null, System. Web. caching. cache. noabsoluteexpiration,
  3. System. Web. caching. cache. noslidingexpiration,
  4. System. Web. caching. cacheitempriority. High,Null);

6. Use the add method to add

The add method returns the object you added to the cache. In addition, if the add method is used and an item with the same name as an existing item already exists in the cache, this method will not replace this item and will not cause an exception.

  1. StringCacheditem = (String) Cache. Add ("cacheitem ",
  2. "Cached item ",Null,
  3. System. Web. caching. cache. noabsoluteexpiration,
  4. System. Web. caching. cache. noslidingexpiration,
  5. System. Web. caching. cacheitempriority. Default,
  6. Null);
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.