Caching common operations

Source: Internet
Author: User

usingSystem;usingSystem.Web.Caching;usingsystem.web;usingSystem.Collections;usingSystem.Text.RegularExpressions;namespacetools{/// <summary>    ///Caching common operations///Autho:songbiao///version:1.4///lastedittime:2015-12-12/// </summary>     Public classCacheutil { PublicCacheutil () {//... Add constructor logic here        }         /// <summary>        ///Add a Cache object/// </summary>        /// <param name= "Strkey" >Key Value name</param>        /// <param name= "Valueobj" >The object being cached</param>        /// <param name= "Durationmin" >cache expiration Time, default is 3 minutes</param>        /// <param name= "priority" >reserved Precedence (enumerated values), 1 is most not cleared, 6 is most easily purged by memory management, 0 is default///"1:notremovable;2:high;3:abovenormal;4:normal;5:belownormal;6:low"</param>        /// <returns>whether the cache write succeeds True, False</returns>         Public Static BOOLInsertcach (stringStrkey,ObjectValueobj,intDurationmin,intPriority )            {TimeSpan ts; if(Strkey! =NULL&& Strkey.length! =0&& Valueobj! =NULL)            {                //create an instance of a callback delegate//OnRemove is the function that the delegate executes, the concrete method looks the following onremove (...)CacheItemRemovedCallback CallBack =NewCacheItemRemovedCallback (onremove); #regionExpiration Time settingif(Durationmin = =0) {TS=NewTimeSpan (0,3,0);//three minutes if not set                }                Else{TS=NewTimeSpan (0, Durationmin,0); }                #endregion                #regionRelative priority of items stored in the System.Web.Caching.Cache objectcacheitempriority cachepriority; Switch(priority) { Case 6: Cachepriority=Cacheitempriority.low;  Break;  Case 5: Cachepriority=Cacheitempriority.belownormal;  Break;  Case 4: Cachepriority=Cacheitempriority.normal;  Break;  Case 3: Cachepriority=Cacheitempriority.abovenormal;  Break;  Case 2: Cachepriority=Cacheitempriority.high;  Break;  Case 1: Cachepriority=cacheitempriority.notremovable;  Break; default: Cachepriority=Cacheitempriority.default;  Break; }                #endregionHttpContext.Current.Cache.Insert (strkey, Valueobj,NULL, DATETIME.NOW.ADD (TS), System.Web.Caching.Cache.NoSlidingExpiration, cachepriority, CallBack); return true; }            Else            {                return false; }        }         /// <summary>        ///determine if the cache object exists/// </summary>        /// <param name= "Strkey" >Cache key Value name</param>        /// <returns>whether there is a true, false</returns>         Public Static BOOLIsexist (stringstrkey) {            returnHttpcontext.current.cache[strkey]! =NULL; }         /// <summary>        ///Reading Cached Objects/// </summary>        /// <param name= "Strkey" >Cache key Value name</param>        /// <returns>Cache object, OBJEC type</returns>         Public Static ObjectGetCache (stringstrkey) {//Remove Value             if(Httpcontext.current.cache[strkey]! =NULL)            {                Objectobj =Httpcontext.current.cache[strkey]; if(obj = =NULL)                {                    return NULL; }                Else                {                    returnobj; }            }            Else            {                return NULL; }        }         /// <summary>        ///To delete a cached object/// </summary>        /// <param name= "Strkey" >Cache key Value name</param>         Public Static voidRemove (stringstrkey) {            if(Httpcontext.current.cache[strkey]! =NULL) {HttpContext.Current.Cache.Remove (strkey); }        }         /// <summary>        ///clears the cache object according to the regular expression set;///the method uses a regular match for the key-value object to be deleted, and if the key value is named uniform, the relevant cache data can be purged by a batch O (∩_∩) o/// </summary>        /// <param name= "pattern" >regular expressions that match key values</param>         Public Static voidRemovebyregexp (stringpattern) {            if(Pattern! ="") {IDictionaryEnumerator enu=HttpContext.Current.Cache.GetEnumerator ();  while(ENU. MoveNext ()) {stringKey =ENU.                    Key.tostring (); if(Regex.IsMatch (key, pattern)) {Remove (key); }                }            }        }         /// <summary>        ///Clear all Cached objects/// </summary>         Public Static voidClear () {IDictionaryEnumerator enu=HttpContext.Current.Cache.GetEnumerator ();  while(ENU. MoveNext ()) {Remove (enu.            Key.tostring ()); }        }          Public StaticCacheItemRemovedReason reason; /// <summary>        ///This method is called before the value is invalidated and can be used to update the database before it expires, or to retrieve the data from the database/// </summary>        /// <param name= "strkey" ></param>        /// <param name= "obj" ></param>        /// <param name= "Reason" ></param>        Private Static voidOnRemove (stringStrkey,Objectobj, CacheItemRemovedReason R) {Reason=R; }    }  }

Caching common operations

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.