Esempio n. 1
0
        /// <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 try to get the item from a
            if (!ContinuousCacheAccessSynchronizationManager.IsCacheItemContainerInitialized(cacheItemName))
            {
                ICacheItem item = _StaticCacheController.GetCacheItem(cacheItemName);

                // throw exception if the element was not found
                if (item == null)
                {
                    throw new CachingException("Cache object \"" + cacheItemName + "\" not found at the CacheElementCollection.");
                }
                if (item.IsClustered)
                {
                    throw new CachingException("Cache Configuration item with key " + cacheItemName + " is configured as clustered. SlimCacheManager does not support clustered cache items. Use CacheManager instead.");
                }

                // get the global cachesettings
                if (item.Minutes < 0 && item.Seconds < 0 && item.LifeSpan.TotalMilliseconds <= 0)
                {
                    item.Minutes = _StaticCacheController.Minutes;
                }

                if (item.LifeSpan == TimeSpan.Zero)
                {
                    throw new CachingException("Cache Configuration item with key " + cacheItemName + " has no expiry time specified. Either set the Seconds or Minutes value. It is allowed to set both values at a time.");
                }

                CacheMode cacheMode;
                if (item.IsMemcached)
                {
                    if (item.UseProtocolBufferSerialization)
                    {
                        cacheMode = CacheMode.MemcachedProtocolBufferSerialization;
                    }
                    else
                    {
                        cacheMode = CacheMode.Memcached;
                    }
                }
                else
                {
                    cacheMode = CacheMode.InProcess;
                }

                CacheItemContainer container = new CacheItemContainer(
                    item
                    , string.IsNullOrEmpty(item.CacheKey) ? item.Name : item.CacheKey
                    , cacheMode
                    , _StaticCacheController.ContinuousAccessStaleKeySuffixForMemcached);

                ContinuousCacheAccessSynchronizationManager.InitializeCacheItemSynchronizationController(container);
            }

            return(ContinuousCacheAccessSynchronizationManager.GetCacheItemContainer(cacheItemName));
        }
        /// <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;

                    HttpRuntime.Cache.Insert(
                        cacheKey
                        , item
                        , null
                        , cacheItemContainer.ActualExpiryDate
                        , System.Web.Caching.Cache.NoSlidingExpiration
                        , cacheItemContainer.CacheItem.CacheItemPriority
                        , null);

                    #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>
        /// Fetches data and inserts it into cache via memcached
        /// </summary>
        public void FetchAndInsertMemcachedData()
        {
            CacheItemContainer cacheItemContainer = ContinuousCacheAccessSynchronizationManager.GetCacheItemContainer(_CacheItemName);

            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)
            {
                SlimCacheManager.InsertMemcachedObject <T>(cacheItemContainer, _IterationKey, item);

                #region debug
#if DEBUG
                Trace.WriteLine(DebugConstants.DEBUG_PREFIX + "SEPARATE THREAD MEMCACHED: Last Fetch at thread finished at " + cacheItemContainer.LatestFetch.ToString("HH:mm:ss.fff"));
#endif
                #endregion
            }
        }