C # data cache Introduction and general Caching help classes

Source: Internet
Author: User

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 ;}}}


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.