Cache Helper Class

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingsystem.web;usingSystem.Collections;usingSystem.Web.Caching;namespacedmsite.common{/// <summary>    ///Server Cache helper Classes/// </summary>     Public classCachehelper {/// <summary>        ///file dependencies for creating cache entries/// </summary>        /// <param name= "key" >Cache Key</param>        /// <param name= "obj" >Object Objects</param>        /// <param name= "FileName" >File Absolute Path</param>         Public Static voidInsertFile (stringKeyObjectObjstringfileName) {            //To Create a cache dependencyCacheDependency dep =NewCacheDependency (fileName); //Create CacheHttpRuntime.Cache.Insert (key, obj, DEP); }        /// <summary>        ///Create cache entry expires/// </summary>        /// <param name= "key" >Cache Key</param>        /// <param name= "obj" >Object Objects</param>         Public Static voidInsert (stringKeyObjectobj) {            if(obj! =NULL)            {                if(Isexist (key)) {Httpruntime.cache[key]=obj; }                Else                {                    intexpires = Converthelper.toint (Confighelper.getappsettings ("Timecache")); HttpRuntime.Cache.Insert (key, obj,NULL, System.Web.Caching.Cache.NoAbsoluteExpiration,NewTimeSpan (0, Expires,0)); }                            }        }        /// <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) {            returnHttpruntime.cache[strkey]! =NULL; }        /// <summary>        ///Get Cache Object/// </summary>        /// <param name= "key" >Cache Key</param>        /// <returns>Object Objects</returns>         Public Static ObjectGetCache (stringkey) {            if(string. IsNullOrEmpty (key))return NULL; if(Confighelper.getappsettings ("Iscache") =="false")            {                return NULL; }            returnHttpRuntime.Cache.Get (key); }        /// <summary>        ///Get Cache Object/// </summary>        /// <typeparam name= "T" >T Object</typeparam>        /// <param name= "key" >Cache Key</param>        /// <returns></returns>         Public StaticT get<t> (stringkey) {            Objectobj =GetCache (key); returnobj = =NULL?default(t): (t) obj; }        /// <summary>        ///remove the specified data cache/// </summary>         Public Static voidRemoveCache (stringCacheKey) {System.Web.Caching.Cache _cache=Httpruntime.cache; _cache.        Remove (CacheKey); }        /// <summary>        ///Removing all caches/// </summary>         Public Static voidRemoveallcache () {System.Web.Caching.Cache _cache=Httpruntime.cache; IDictionaryEnumerator Cacheenum=_cache.            GetEnumerator ();  while(Cacheenum.movenext ()) {_cache.            Remove (CacheEnum.Key.ToString ()); }        }    }}

Cache Helper Class

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.