HttpRuntime. Cache data in ASP. NET,

Source: Internet
Author: User

HttpRuntime. Cache data in ASP. NET,

Recently, I started a development project and found that the number of Access_Token requests per day is limited. Then I thought of caching. I just saw the HttpRuntime. Cache recommended in the tutorial.

A Copy is written as a helper class, which currently only includes creating, obtaining, and clearing

The following code is used:

1 using System; 2 using System. collections; 3 using System. collections. generic; 4 using System. linq; 5 using System. web; 6 using System. web. caching; 7 8 namespace TEST. public 9 {10 public class CacheHelper11 {12 13 // <summary> 14 // create cache 15 /// </summary> 16 /// <param name = "key "> cached Key </param> 17 // <param name =" value "> cached data </param> 18 /// <param name =" cacheDependency "> dependency, generally null </param> 19 // <pa Ram name = "dateTime"> cache expiration time </param> 20 // <param name = "timeSpan"> set whether the cache is expired or expires after the expiration time </param> 21 // <param name = "cacheItemPriority"> cache priority </param> 22 // <param name = "cacheItemRemovedCallback"> callback method, generally null </param> 23 // <returns> </returns> 24 public bool CreateCache (string key, object value, CacheDependency cacheDependency, DateTime dateTime, TimeSpan timeSpan, 25 CacheItemPriority cacheItemPriority, Cac HeItemRemovedCallback cacheItemRemovedCallback) 26 {27 if (string. isNullOrEmpty (key) | value = null) 28 {29 return false; 30} 31 HttpRuntime. cache. insert (key, value, cacheDependency, dateTime, timeSpan, cacheItemPriority, cacheItemRemovedCallback); 32 return true; 33} 34 35 // <summary> 36 // get cache 37 // </summary> 38 // <param name = "key"> </param> 39 /// <returns> </returns> 40 public object GetCache (st Ring key) 41 {42 return string. IsNullOrEmpty (key )? Null: HttpRuntime. cache. get (key ); 43} 44 45 46 // <summary> 47 // remove all caches 48 // </summary> 49 // <returns> </returns> 50 public bool removeAll () 51 {52 IDictionaryEnumerator iDictionaryEnumerator = HttpRuntime. cache. getEnumerator (); 53 while (iDictionaryEnumerator. moveNext () 54 {55 HttpRuntime. cache. remove (Convert. toString (iDictionaryEnumerator. key); 56} 57 return true; 58} 59} 60}

 

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.