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