Sitecore Cache Manager

Source: Internet
Author: User

namespaceXXX. shared.infrastructure.caching{usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Runtime.Caching; usingSystem.Threading; usingSitecore.data; usingSitecore.diagnostics; usingsitecore.sites; /// <summary>    ///The cache manager. /// </summary>     Public Sealed classCacheManager {#regionStatic fieldsPrivate Static ReadOnlyLazy<cachemanager> Instance =NewLazy<cachemanager> (() =NewCacheManager (), lazythreadsafetymode.executionandpublication); Private Static ReadOnly ObjectSyncLock =New Object(); #endregion        #regionFieldsPrivate ReadOnlyObjectCache cache =Memorycache.default; #endregion        #regionPublic Properties/// <summary>        ///Gets the current. /// </summary>         Public StaticCacheManager Current {Get{returnInstance.value;} }        #endregion        #regionPublic Methods and Operators/// <summary>        ///The Add. /// </summary>        /// <param name= "key" >        ///The key. /// </param>        /// <param name= "Data" >        ///The data. /// </param>        /// <param name= "expirytimeinhours" >        /// </param>        /// <typeparam name= "T" >        /// </typeparam>         Public voidAdd<t> (stringKey, T data,DoubleExpirytimeinhours =6)whereT:class        {            Try            {                if(Data = =NULL)                {                    Throw NewArgumentNullException ("Data","cannot add null to the cache."); }                Lock(SyncLock) {varPolicy =NewCacheItemPolicy {absoluteexpiration=DateTime.Now.AddHours (expirytimeinhours), priority=Cacheitempriority.default, slidingexpiration=TimeSpan.Zero};  This. Cache.                ADD (key, data, policy); }            }            Catch(Exception ex) {Log.debug (ex). Message, This); }        }        /// <summary>        ///The contains key. /// </summary>        /// <param name= "key" >        ///The key. /// </param>        /// <returns>        /// the<see cref= "bool"/>. /// </returns>         Public BOOLContainsKey (stringkey) {            Lock(SyncLock) {return  This. Cache.            Contains (key); }        }        /// <summary>        ///The get. /// </summary>        /// <param name= "key" >        ///The key. /// </param>        /// <typeparam name= "T" >        /// </typeparam>        /// <returns>        /// the<see cref= "T"/>. /// </returns>         PublicT get<t> (stringKeywhereT:class        {            Lock(SyncLock) {return  This. ContainsKey (key)? This. cache. Get (Key) asT:default(T); }        }        /// <summary>        ///The get all cache keys. /// </summary>        /// <returns>        /// the<see cref= "Ienumerable{t}"/>. /// </returns>         Publicienumerable<string>Getallcachekeys () {return  This. cache. Select (item =item. Key).        ToList (); }        /// <summary>        ///The Purge. /// </summary>         Public voidPurge () {Lock(SyncLock) {varKeys = This. cache. Select (item =item.                Key); Keys. ToList (). ForEach ( This.            Remove); }        }        /// <summary>        ///The Remove. /// </summary>        /// <param name= "key" >        ///The key. /// </param>         Public voidRemove (stringkey) {            Lock(SyncLock) {if(! This. ContainsKey (key)) {return; }                 This. Cache.            Remove (key); }        }         Public voidRemovesitecoreitemcache (stringIdstringsiteName) {            if(!string. IsNullOrEmpty (ID) &&!string. IsNullOrEmpty (SiteName)) {using(NewSitecontextswitcher (Sitecontextfactory.getsitecontext (siteName))) {                    vardb =SiteContext.Current.Database; if(db! =NULL) {db. Caches.ItemCache.RemoveItem (NewID (ID)); Db. Caches.DataCache.RemoveItemInformation (NewID (ID)); Db.                    Caches.StandardValuesCache.RemoveKeysContaining (ID); }                }            }        }        /// <summary>        ///The remove by prefix. /// </summary>        /// <param name= "prefix" >        ///The prefix. /// </param>         Public voidRemovebyprefix (stringprefix) {            Lock(SyncLock) {varKeys = This. cache. Where (item = Item. Key.indexof (prefix, stringcomparison.ordinalignorecase)! =-1); Keys. ToList (). ForEach (Item= This. Remove (item.            Key)); }        }        #endregion    }}

Sitecore Cache Manager

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.