Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Determines whether [is object cached] [the specified cache item name].
        /// </summary>
        /// <param name="cacheItemName">Name of the cache item.</param>
        /// <param name="iterationKey">The iteration key.</param>
        /// <returns>
        ///     <c>true</c> if [is object cached] [the specified cache item name]; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsObjectCached(string cacheItemName, string iterationKey)
        {
            CacheItemContainer cacheItemContainer = CreateSaveItemInstance(cacheItemName);

            if (cacheItemContainer != null)
            {
                return(_Cache[cacheItemContainer.CacheKey + iterationKey ?? string.Empty] != null);
            }

            return(false);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Purges the cache item.
        /// </summary>
        /// <param name="cacheItemName">Name of the cache item.</param>
        /// <param name="iterationKey">The iteration key.</param>
        public static void PurgeCacheItem(string cacheItemName, string iterationKey)
        {
            CacheItemContainer cacheItemContainer = CreateSaveItemInstance(cacheItemName);

            if (cacheItemContainer != null)
            {
                _Cache.Remove(cacheItemContainer.CacheKey + iterationKey ?? string.Empty);

                #region debug
#if DEBUG
                Trace.WriteLine(DebugConstants.DEBUG_PREFIX + "Removed item " + cacheItemContainer.CacheKey + " from cache");
#endif
                #endregion
            }
        }
        public static void InitializeCacheItemSynchronizationController(CacheItemContainer cacheItemContainer)
        {
            if (!_FetchDataLockTable.ContainsKey(cacheItemContainer.CacheKey))
            {
                lock (_FetchDataLockTable)
                {
                    if (!_FetchDataLockTable.ContainsKey(cacheItemContainer.CacheKey))
                    {
                        _FetchDataLockTable.Add(cacheItemContainer.CacheKey, new object());
                        _IsFetchingDataLookup.Add(cacheItemContainer.CacheKey, false);

                        _CacheItemNameLocks.Add(cacheItemContainer.CacheItem.Name, new object());
                        _CacheItems.Add(cacheItemContainer.CacheItem.Name, cacheItemContainer);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        /// <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);
        }
Ejemplo n.º 6
0
        /// <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);
        }
        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);
            }
        }