static SlimCacheManager() { _StaticCacheController = CacheControllerFactory.CreateCacheController(); _Cache = MemoryCache.Default; CacheItemContainer container; foreach (ICacheItem item in _StaticCacheController.IterateOverCacheItems()) { // get the global cachesettings if (item.LifeSpan.TotalMilliseconds <= 0) { item.LifeSpan = _StaticCacheController.LifeSpanInterval; } if (item.LifeSpan == TimeSpan.Zero) { throw new CachingException("Cache Configuration item with key " + item.Name + " has no expiry time specified. Either set the Seconds or Minutes value. It is allowed to set both values at a time."); } container = new CacheItemContainer( item , string.IsNullOrEmpty(item.CacheKey) ? item.Name : item.CacheKey); ContinuousCacheAccessSynchronizationManager.InitializeCacheItemSynchronizationController(container); } }
public static TriggerAsynchronousFetchStatus TriggerAsynchronousFetch <T>(LoadSerializedObjectDelegate <T> loadObject, string cacheItemName , string iterationKey, CacheItemContainer cacheItemContainer) where T : class { ContinuousCacheAccessThreadHelper <T> threadHelper; Thread thread; // we are in the extended lifespan, so we need to check whether we have to reload the object if (ContinuousCacheAccessSynchronizationManager.SetIsFetchingDataFlagToTrue(cacheItemContainer.CacheKey)) { #region debug #if DEBUG Trace.WriteLine(DebugConstants.DEBUG_PREFIX + "TRIGGER ASYNCHRONOUS FETCH: Initialize asynchronous fetch"); #endif #endregion threadHelper = ContinuousCacheAccessThreadHelper <T> .GetInstance(loadObject, cacheItemName, iterationKey); thread = new Thread(threadHelper.FetchAndInsertData); thread.Start(); return(TriggerAsynchronousFetchStatus.SucessfullyInitialized); } else { #region debug #if DEBUG Trace.WriteLine(DebugConstants.DEBUG_PREFIX + "TRIGGER ASYNCHRONOUS FETCH: Already in process..."); #endif #endregion return(TriggerAsynchronousFetchStatus.AlreadyFetching); } }
/// <summary> /// Fetches data and inserts it into cache /// </summary> public void FetchAndInsertData() { CacheItemContainer cacheItemContainer = ContinuousCacheAccessSynchronizationManager.GetCacheItemContainer(_CacheItemName); string cacheKey = cacheItemContainer.CacheKey + _IterationKey; ContinuousCacheAccessSynchronizationManager.SetIsFetchingDataFlag(cacheKey, true); lock (ContinuousCacheAccessSynchronizationManager.CacheItemNameLocks[cacheItemContainer.CacheItem.Name]) { #region debug #if DEBUG Trace.WriteLine(DebugConstants.DEBUG_PREFIX + "SEPARATE THREAD: Starting FetchAndInsertData at " + DateTime.UtcNow.ToString("HH:mm:ss.fff")); #endif #endregion T item = default(T); try { item = _LoadObjectDelegate.Invoke(); } catch (Exception e) { throw new CachingException("An error occurred during the execution of the loadObject delegate. See the inner exception for further details.", e); } if (item != null) { cacheItemContainer.LatestFetch = DateTime.UtcNow; CacheItemPolicy cacheItemPolicy = new CacheItemPolicy(); cacheItemPolicy.Priority = cacheItemContainer.CacheItem.CacheItemPriority; cacheItemPolicy.AbsoluteExpiration = cacheItemContainer.ActualExpiryDate; SlimCacheManager._Cache.Set( cacheKey , item , cacheItemPolicy); #region debug #if DEBUG Trace.WriteLine(DebugConstants.DEBUG_PREFIX + "\n***\n*INPROCESS CACHE INSERT:\n*\t\t\tInserted new object into cache at " + cacheItemContainer.LatestFetch.ToString("HH:mm:ss.fff") + "\n*\t\t\tActual expiry date: " + cacheItemContainer.ActualExpiryDate.ToString("HH:mm:ss.fff") + "\n*\t\t\tInserted new stale key with expiry date: " + cacheItemContainer.SpecifiedExpiryDate.ToString("HH:mm:ss.fff") + "\n************************************************************************************************\n"); #endif #endregion } } ContinuousCacheAccessSynchronizationManager.SetIsFetchingDataFlag(cacheKey, false); }
/// <summary> /// Creates the save item instance. /// </summary> /// <param name="cacheItemName">Name of the cache item.</param> /// <returns></returns> private static CacheItemContainer CreateSaveItemInstance(string cacheItemName) { // we don't check whether the specified name acutally exists but just throw a key not found exception return(ContinuousCacheAccessSynchronizationManager.GetCacheItemContainer(cacheItemName)); }
/// <summary> /// Loads from cache. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="cacheItemName">Name of the cache item.</param> /// <param name="iterationKey">The iteration key.</param> /// <param name="loadObject">The load object.</param> /// <returns></returns> public static T LoadFromCache <T>(string cacheItemName, string iterationKey, LoadSerializedObjectDelegate <T> loadObject) where T : class { T returnObject = default(T); // get the section CacheItemContainer cacheItemContainer = CreateSaveItemInstance(cacheItemName); string cacheKey = cacheItemContainer.CacheKey + iterationKey ?? string.Empty; if (_StaticCacheController.Enabled && cacheItemContainer.CacheItem.Enabled) {// use cache #region InProcess returnObject = _Cache[cacheKey] as T; if (returnObject == null) { lock (ContinuousCacheAccessSynchronizationManager.CacheItemNameLocks[cacheItemName]) { // check again in case the object was inserted into cache after the conditional statement but before the Monitor lock returnObject = _Cache[cacheKey] as T; if (returnObject == null) {// get new object and cache it #region debug #if DEBUG Trace.WriteLine(DebugConstants.DEBUG_PREFIX + "Item not found at cache"); #endif #endregion try { returnObject = loadObject.Invoke(); } catch (Exception e) { throw new CachingException("An error occurred during the execution of the loadObject delegate. See the inner exception for further details.", e); } if (returnObject != null) { cacheItemContainer.LatestFetch = DateTime.UtcNow; CacheItemPolicy cacheItemPolicy = new CacheItemPolicy(); cacheItemPolicy.Priority = cacheItemContainer.CacheItem.CacheItemPriority; cacheItemPolicy.AbsoluteExpiration = cacheItemContainer.ActualExpiryDate; SlimCacheManager._Cache.Set( cacheKey , returnObject , cacheItemPolicy); #region debug #if DEBUG Trace.WriteLine("\n" + DebugConstants.DEBUG_PREFIX + "\n*INPROCESS CACHE INSERT:\n*\t\t\tInserted new object into cache at " + cacheItemContainer.LatestFetch.ToString("HH:mm:ss.fff") + "\n*\t\t\tActual expiry date: " + cacheItemContainer.ActualExpiryDate.ToString("HH:mm:ss.fff") + "\n*\t\t\tInserted new stale key with expiry date: " + cacheItemContainer.SpecifiedExpiryDate.ToString("HH:mm:ss.fff") + "\n*\t\t\tKey: " + cacheItemContainer.CacheKey + "\n************************************************************************************************\n"); #endif #endregion } #region debug #if DEBUG Trace.WriteLine(DebugConstants.DEBUG_PREFIX + "Item inserted at normal cache call"); #endif #endregion } } } else { if (cacheItemContainer.CacheItem.UseContinuousAccess && cacheItemContainer.IsInExtendedLifeSpan(DateTime.UtcNow)) { #region debug #if DEBUG Trace.WriteLine(DebugConstants.DEBUG_PREFIX + "IsInExtendedLifeSpan at " + DateTime.UtcNow.ToString("HH:mm:ss.fff")); #endif #endregion // we are in the extended lifespan, so we need to check whether we have to reload the object ContinuousCacheAccessSynchronizationManager.TriggerAsynchronousFetch <T>(loadObject, cacheItemName, iterationKey, cacheItemContainer); } } #endregion } else {// don't use cache try { returnObject = loadObject.Invoke(); } 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); }