示例#1
0
        private static object GetCachedDataFromDictionary(CacheItemArgs cacheItemArgs, CacheItemExpiredCallback cacheItemExpired)
        {
            object cachedObject;

            bool isFound;

            using (dictionaryCache.GetReadLock())
            {
                isFound = dictionaryCache.TryGetValue(cacheItemArgs.CacheKey, out cachedObject);
            }

            if (!isFound)
            {
                // get object from data source using delegate
                try
                {
                    cachedObject = cacheItemExpired != null?cacheItemExpired(cacheItemArgs) : null;
                }
                catch (Exception ex)
                {
                    cachedObject = null;
                    Exceptions.LogException(ex);
                }

                using (dictionaryCache.GetWriteLock())
                {
                    if (!dictionaryCache.ContainsKey(cacheItemArgs.CacheKey))
                    {
                        if (cachedObject != null)
                        {
                            dictionaryCache[cacheItemArgs.CacheKey] = cachedObject;
                        }
                    }
                }
            }

            return(cachedObject);
        }
示例#2
0
 public static TObject GetCachedObject <TObject>(CacheItemArgs cacheItemArgs, CacheItemExpiredCallback cacheItemExpired, bool saveInDictionary)
 {
     return(DataCache.GetCachedData <TObject>(cacheItemArgs, cacheItemExpired, saveInDictionary));
 }
示例#3
0
 /// -----------------------------------------------------------------------------
 /// <summary>
 /// GetCachedObject gets an object from the Cache
 /// </summary>
 /// <typeparam name="TObject">The type of th object to fetch</typeparam>
 /// <param name="cacheItemArgs">A CacheItemArgs object that provides parameters to manage the
 /// cache AND to fetch the item if the cache has expired</param>
 /// <param name="cacheItemExpired">A CacheItemExpiredCallback delegate that is used to repopulate
 /// the cache if the item has expired</param>
 /// -----------------------------------------------------------------------------
 public static TObject GetCachedObject <TObject>(CacheItemArgs cacheItemArgs, CacheItemExpiredCallback cacheItemExpired)
 {
     return(DataCache.GetCachedData <TObject>(cacheItemArgs, cacheItemExpired));
 }
示例#4
0
        private static object GetCachedDataFromRuntimeCache(CacheItemArgs cacheItemArgs, CacheItemExpiredCallback cacheItemExpired)
        {
            object objObject = GetCache(cacheItemArgs.CacheKey);

            // if item is not cached
            if (objObject == null)
            {
                //Get Unique Lock for cacheKey
                object @lock = GetUniqueLockObject(cacheItemArgs.CacheKey);

                // prevent other threads from entering this block while we regenerate the cache
                lock (@lock)
                {
                    // try to retrieve object from the cache again (in case another thread loaded the object since we first checked)
                    objObject = GetCache(cacheItemArgs.CacheKey);

                    // if object was still not retrieved

                    if (objObject == null)
                    {
                        // get object from data source using delegate
                        try
                        {
                            objObject = cacheItemExpired(cacheItemArgs);
                        }
                        catch (Exception ex)
                        {
                            objObject = null;
                            Exceptions.LogException(ex);
                        }

                        // set cache timeout
                        int timeOut = cacheItemArgs.CacheTimeOut * Convert.ToInt32(Host.PerformanceSetting);

                        // if we retrieved a valid object and we are using caching
                        if (objObject != null && timeOut > 0)
                        {
                            // save the object in the cache
                            SetCache(cacheItemArgs.CacheKey,
                                     objObject,
                                     cacheItemArgs.CacheDependency,
                                     Cache.NoAbsoluteExpiration,
                                     TimeSpan.FromMinutes(timeOut),
                                     cacheItemArgs.CachePriority,
                                     cacheItemArgs.CacheCallback);

                            // check if the item was actually saved in the cache

                            if (GetCache(cacheItemArgs.CacheKey) == null)
                            {
                                // log the event if the item was not saved in the cache ( likely because we are out of memory )
                                var log = new LogInfo {
                                    LogTypeKey = EventLogController.EventLogType.CACHE_OVERFLOW.ToString()
                                };
                                log.LogProperties.Add(new LogDetailInfo(cacheItemArgs.CacheKey, "Overflow - Item Not Cached"));
                                LogController.Instance.AddLog(log);
                            }
                        }

                        //This thread won so remove unique Lock from collection
                        RemoveUniqueLockObject(cacheItemArgs.CacheKey);
                    }
                }
            }

            return(objObject);
        }
示例#5
0
 public static TObject GetCachedData <TObject>(CacheItemArgs cacheItemArgs, CacheItemExpiredCallback cacheItemExpired)
 {
     // declare local object and try and retrieve item from the cache
     return(GetCachedData <TObject>(cacheItemArgs, cacheItemExpired, false));
 }