ASP. NET Cache helper Class

Source: Internet
Author: User

Helper Class Code

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.Caching;namespacebusinessrules.common{/// <summary>    ///Cache Height Caching/// </summary>     Public classCacheManager {#region--property--Private Static string_cachekey =String.Empty; Private StaticString _sqldepname ="CacheData"; /// <summary>        ///Cache index Key Values/// </summary>         Public Static stringCacheKey {Get{returnCachemanager._cachekey;} Set{Cachemanager._cachekey =value;} }        /// <summary>        ///Cache dependent name/// </summary>         Public StaticString Sqldepname {Get{returnCachemanager._sqldepname;} Set{Cachemanager._sqldepname =value;} }        #endregion        #region--method--/// <summary>        ///Gets the cache object value for the current application specified CacheKey/// </summary>        /// <param name= "Cachekety" >index key value</param>        /// <returns>return Object</returns>         Public StaticObject GetCache (String cacheKey) {if(!String.IsNullOrEmpty (CacheKey)) {Cache Objcache=Httpruntime.cache; returnObjcache[cachekey]; }            Else            {                return NULL; }        }        /// <summary>        ///Gets the cache object value for the current application specified CacheKey/// </summary>        /// <typeparam name= "T" >Object Type</typeparam>        /// <param name= "CacheKey" >index key value</param>        /// <returns>return Object Type</returns>         Public StaticT getcache<t>(String cacheKey) {Objectobj =GetCache (CacheKey); returnobj = =NULL?default(t): (t) obj; }        /// <summary>        ///sets the cache object value for the current application to specify CacheKey/// </summary>        /// <param name= "CacheKey" >index key value</param>        /// <param name= "obj" >C</param>         Public Static voidSetcache (String cacheKey, Object obj) {Cache Objcache=Httpruntime.cache;        Objcache.insert (CacheKey, obj); }        /// <summary>        ///To Create a cache dependency/// </summary>        /// <param name= "CacheKey" ></param>        /// <param name= "obj" ></param>        /// <param name= "FileName" ></param>         Public Static voidSetcache (String cacheKey, Object obj, string fileName) {CacheDependency CDEP=NewCacheDependency (fileName); Cache Objcache=Httpruntime.cache;        Objcache.insert (CacheKey, obj, CDEP); }        /// <summary>        ///sets the cache object value for the current application to specify CacheKey/// </summary>        /// <param name= "CacheKey" >index key value</param>        /// <param name= "obj" >index key value</param>        /// <param name= "absoluteexpiration" >Absolute Expiration Time</param>        /// <param name= "slidingexpiration" >the time interval between when the inserted object was last accessed and when the object expired</param>         Public Static voidSetcache (String cacheKey, Object obj, DateTime absoluteexpiration, TimeSpan slidingexpiration) {C Ache Objcache=Httpruntime.cache; Objcache.insert (CacheKey, obj,NULL, absoluteexpiration, slidingexpiration); }        /// <summary>        ///Create cache entry expires/// </summary>        /// <param name= "CacheKey" >index key value</param>        /// <param name= "obj" >Indexed Objects</param>        /// <param name= "Expires" >Expiry Time</param>         Public Static voidSetcache (String cacheKey, Object obj, Int32 expires) {Cache Objcache=Httpruntime.cache; Objcache.insert (CacheKey, obj,NULL, Cache.noabsoluteexpiration,//never Expires            NewTimeSpan (0, Expires,0)            ); }        /// <summary>        ///setting up dependent ways to cache data/// </summary>        /// <param name= "CacheKey" ></param>        /// <param name= "obj" ></param>        /// <param name= "CDEP" ></param>         Public Static voidSetcache (String cacheKey, Object obj, CacheDependency cdep) {Cache Objcache=Httpruntime.cache; Objcache.insert (CacheKey, obj, CDEP, Cache.noabsoluteexpirati On,//never ExpiresCache.noslidingexpiration,//Disable Adjustable expirationCacheitempriority.default,NULL                ); }        //========== Call mode start===============//file Dependent usage Instructions//System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency ("C:\\Test.txt"); //Setcache (CacheKey, Objmodel, DEP);//Write Cache//        //Database Dependency Usage Instructions//System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency ("Codematic", "p_product")        ; //Setcache (CacheKey, Objmodel, DEP);//Write Cache//        //========== Call mode end===============        #endregion    }}

Invocation mode

        //set the cached key valueCachemanager.cachekey ="navmenue"; //gets the cached data under the current key valueObject Cachemodel =Cachemanager.getcache (Cachemanager.cachekey); //if the current key-value object does not exist in the cache            if(Cachemodel = =NULL)            {                //storing data in the cacheCachemodel =bsnmanage.obj_list (navposition); if(Cachemodel! =NULL)                {                    //Dependent database sc_navigation table changes to updateSqlCacheDependency SCDEP =NewSqlCacheDependency (Cachemanager.sqldepname,"sc_navigation"); //Write CacheCachemanager.setcache (Cachemanager.cachekey, Cachemodel, SCDEP); }                return(DataTable) Cachemodel; }            Else                return(DataTable) Cachemanager.getcache (Cachemanager.cachekey);

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.