C # data cache Introduction and general Caching help classes
C # cache is mainly used to speed up Data Reading. Because there is a traffic bottleneck between the server and the application client, when reading large data volumes, cache is used to serve the client directly, which can reduce the data interaction between the client and the server, this greatly improves the program performance.
The following are common class libraries used for cache operations:
Using System; using System. collections; using System. collections. generic; using System. linq; using System. web; using System. web. caching; using System. web. hosting; namespace ECS. utility {////// Brief description of Caching ///Public class Caching {////// Obtain the Cache value of the specified CacheKey of the current application /////////
Y public static object GetCache (string CacheKey) {System. Web. Caching. Cache objCache = HttpRuntime. Cache; return objCache [CacheKey];} ///// Set the Cache value of the specified CacheKey for the current application /////////Public static void SetCache (string CacheKey, object objObject) {System. Web. Caching. Cache objCache = HttpRuntime. Cache; objCache. Insert (CacheKey, objObject );}////// Set the Cache value of the specified CacheKey for the current application /////////Public static void SetCache (string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration) {System. web. caching. cache objCache = HttpRuntime. cache; objCache. insert (CacheKey, objObject, null, absoluteExpiration, slidingExpiration );}////// Clear a single key cache //////Public static void RemoveKeyCache (string CacheKey) {try {System. Web. Caching. Cache objCache = HttpRuntime. Cache; objCache. Remove (CacheKey);} catch {}}////// Clear all caches ///Public static void RemoveAllCache () {System. web. caching. cache _ cache = HttpRuntime. cache; IDictionaryEnumerator CacheEnum = _ cache. getEnumerator (); if (_ cache. count> 0) {ArrayList al = new ArrayList (); while (CacheEnum. moveNext () {al. add (CacheEnum. key);} foreach (string key in al) {_ cache. remove (key );}}}////// Return all existing caches in the form of a list //////
Public static ArrayList ShowAllCache () {ArrayList al = new ArrayList (); System. web. caching. cache _ cache = HttpRuntime. cache; if (_ cache. count> 0) {IDictionaryEnumerator CacheEnum = _ cache. getEnumerator (); while (CacheEnum. moveNext () {al. add (CacheEnum. key) ;}} return al ;}}}