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}