private static void PurgeItemFromCache(string name, string iterationKey) { ICacheItem item = CreateSaveItemInstance(CacheControllerSection, name, iterationKey); if (item.IsClustered) { ClusteredCacheManager.PurgeCacheItem(GetCacheKey(item, iterationKey)); } else { HttpRuntime.Cache.Remove(GetCacheKey(item, iterationKey)); } }
/// <summary> /// Loads the object from cache. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="name">The name.</param> /// <param name="iterationKey">The iteration key.</param> /// <param name="loadObject">The load object.</param> /// <returns></returns> private static T LoadObjectFromCache <T>(string name, string iterationKey, LoadSerializedObjectDelegate <T> loadObject) { T returnObject = default(T); // get the section ICacheController section = CacheControllerSection; ICacheItem item = CreateSaveItemInstance(section, name, iterationKey); string cacheKey = GetCacheKey(item, iterationKey); if (section.Enabled && item.Enabled) {// use cache if (item.IsClustered) { try { returnObject = (T)ClusteredCacheManager.GetCacheItem( cacheKey , delegate { return(loadObject()); } , item.CacheItemPriority , true); } catch (Exception e) { throw new CachingException("An error occurred during the retrieval of the business object from the cache collection. See the inner exception for further details.", e); } } else { lock (_CacheLock) { if (HttpRuntime.Cache[cacheKey] == null) {// get new object and cache it try { if (item.Minutes < 0 && item.Seconds < 0) { throw new CachingException("You must specify either a seconds or minutes value for a non clusterd cache item."); } DateTime expire = item.Seconds < 0 ? DateTime.UtcNow.AddMinutes(item.Minutes) : DateTime.UtcNow.AddSeconds(item.Seconds); returnObject = loadObject(); if (returnObject != null) { HttpRuntime.Cache.Remove(cacheKey); HttpRuntime.Cache.Insert(cacheKey, returnObject, null, expire, System.Web.Caching.Cache.NoSlidingExpiration, item.CacheItemPriority, null); } } catch (Exception e) { throw new CachingException("An error occurred during the execution of the loadObject delegate. See the inner exception for further details.", e); } } else {// get object from cache try { returnObject = (T)HttpRuntime.Cache[cacheKey]; } catch (Exception e) { throw new CachingException("An error occurred during the retrieval of the business object from the cache collection. See the inner exception for further details.", e); } } } } } else {// don't use cache try { returnObject = loadObject(); } catch (Exception e) { throw new CachingException("An error occurred during the execution of the loadObject delegate. See the inner exception for further details.", e); } } return(returnObject); }