/// <summary> /// Gets the instance. /// </summary> /// <param name="loadObject">The load object.</param> /// <param name="cacheItemName">Name of the cache item.</param> /// <param name="iterationKey">The iteration key.</param> /// <returns></returns> internal static ContinuousCacheAccessThreadHelper <T> GetInstance(LoadSerializedObjectDelegate <T> loadObject, string cacheItemName, string iterationKey) { ContinuousCacheAccessThreadHelper <T> record = new ContinuousCacheAccessThreadHelper <T>(); record._LoadObjectDelegate = loadObject; record._CacheItemName = cacheItemName; record._IterationKey = iterationKey ?? string.Empty; return(record); }
public static TriggerAsynchronousFetchStatus TriggerAsynchronousFetch <T>(LoadSerializedObjectDelegate <T> loadObject, string cacheItemName , string iterationKey, CacheItemContainer cacheItemContainer) where T : class { ContinuousCacheAccessThreadHelper <T> threadHelper; Thread thread; switch (cacheItemContainer.CacheMode) { case CacheMode.InProcess: // 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); } case CacheMode.Memcached: case CacheMode.MemcachedProtocolBufferSerialization: DateTime temporaryExpiry = DateTime.UtcNow.Add(cacheItemContainer.CacheItem.LifeSpan); #region debug #if DEBUG Trace.WriteLine(DebugConstants.DEBUG_PREFIX + "TRIGGER ASYNCHRONOUS FETCH MEMCACHED: Try to add stale key with temporary expiry date " + temporaryExpiry.ToString("HH:mm:ss.fff")); #endif #endregion if (DistributedCache.Add(cacheItemContainer.MemcachedStaleCacheKey, new object(), temporaryExpiry)) { // great, we have to reload now #region debug #if DEBUG Trace.WriteLine(DebugConstants.DEBUG_PREFIX + "TRIGGER ASYNCHRONOUS FETCH MEMCACHED: Initialize asynchronous fetch"); #endif #endregion threadHelper = ContinuousCacheAccessThreadHelper <T> .GetInstance(loadObject, cacheItemName, iterationKey); thread = new Thread(threadHelper.FetchAndInsertMemcachedData); thread.Start(); return(TriggerAsynchronousFetchStatus.SucessfullyInitialized); } else { #region debug #if DEBUG Trace.WriteLine(DebugConstants.DEBUG_PREFIX + "TRIGGER ASYNCHRONOUS FETCH MEMCACHED: Already in process..."); #endif #endregion return(TriggerAsynchronousFetchStatus.AlreadyFetching); } default: throw new ArgumentException("CacheMode " + cacheItemContainer.CacheMode + " not allowed"); } }