internal static TObject GetCachedData <TObject>(CacheItemArgs cacheItemArgs, CacheItemExpiredCallback cacheItemExpired, bool storeInDictionary) { object objObject = null; if (storeInDictionary) { //Use Thread Safe SharedDictionary objObject = GetCachedDataFromDictionary(cacheItemArgs, cacheItemExpired); } else { //Use Cache objObject = GetCachedDataFromRuntimeCache(cacheItemArgs, cacheItemExpired); } // return the object if (objObject == null) { return(default(TObject)); } else { return((TObject)objObject); } }
public TObject GetCachedData <TObject>(CacheItemArgs cacheItemArgs, CacheItemExpiredCallback <TObject> cacheItemExpired) { var objObject = GetCachedDataInternal <TObject>(cacheItemArgs, cacheItemExpired); // return the object if (objObject == null) { return(default(TObject)); } return(objObject); }
internal static TObject GetCachedData <TObject>(CacheItemArgs cacheItemArgs, CacheItemExpiredCallback cacheItemExpired, bool storeInDictionary) { object objObject = storeInDictionary ? GetCachedDataFromDictionary(cacheItemArgs, cacheItemExpired) : GetCachedDataFromRuntimeCache(cacheItemArgs, cacheItemExpired); // return the object if (objObject == null) { return(default(TObject)); } return((TObject)objObject); }
public T GetCachedDataLocalDistributed <T>(string cacheKey, CacheItemExpiredCallback <T> cacheItemExpiredCallBack, int expireMinutes = 0) { string strCacheLocalKey = cacheKey + "_$local"; string strCacheRemoteKey = cacheKey + "_$remote"; var objRemoteVersion = GetCachedData <string>(new CacheItemArgs(strCacheRemoteKey, 0, expireMinutes), (args) => { return(Guid.NewGuid().ToString()); }); var objLocalVersion = GetCache <string>(strCacheLocalKey, level: ProviderLevel.High); if (objLocalVersion != objRemoteVersion) { RemoveCache(cacheKey, ProviderLevel.High); } var cachedData = GetCachedData <T>(new CacheItemArgs(cacheKey, 0, expireMinutes, ProviderLevel.High), (args) => { DateTimeOffset?expireTime = expireMinutes == 0 ? (DateTimeOffset?)null : DateTimeOffset.Now.AddMinutes(expireMinutes); SetCache(strCacheLocalKey, objRemoteVersion, AbsoluteExpiration: expireTime, level: ProviderLevel.High); return(cacheItemExpiredCallBack(args)); }); return(cachedData); }
public static TObject GetCachedObject <TObject>(CacheItemArgs cacheItemArgs, CacheItemExpiredCallback cacheItemExpired, bool saveInDictionary) { return(DataCache.GetCachedData <TObject>(cacheItemArgs, cacheItemExpired, saveInDictionary)); }
/// ----------------------------------------------------------------------------- /// <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)); }
public TObject GetCachedObject <TObject>(CacheItemArgs cacheItemArgs, CacheItemExpiredCallback cacheItemExpired, bool saveInDictionary) { return((TObject)cacheItemExpired(cacheItemArgs)); }
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)); }
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); }
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); }
private TObject GetCachedDataInternal <TObject>(CacheItemArgs cacheItemArgs, CacheItemExpiredCallback <TObject> cacheItemExpired) { TObject objObject = GetCache <TObject>(cacheItemArgs.CacheKey, cacheItemArgs.Scope, cacheItemArgs.Level); // if item is not cached if (EqualityComparer <TObject> .Default.Equals(objObject, default(TObject))) { //Get Unique Lock for cacheKey object @lock = UniqueObject.GetUniqueObject <object>(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 <TObject>(cacheItemArgs.CacheKey, cacheItemArgs.Scope, cacheItemArgs.Level); // if object was still not retrieved if (EqualityComparer <TObject> .Default.Equals(objObject, default(TObject))) { // get object from data source using delegate //try //{ objObject = cacheItemExpired(cacheItemArgs); _logger.LogInformation("GetCacheItemFromExpiredCallback:" + cacheItemArgs.CacheKey); //} //catch (Exception ex) //{ // objObject = default(TObject); // _logger.LogError(ex, "CacheItemExpiredCallbackError"); // } // if we retrieved a valid object and we are using caching if (objObject != null) { // save the object in the cache SetCache(cacheItemArgs.CacheKey, objObject, cacheItemArgs.Scope, cacheItemArgs.ExpireTimeOutMinutes > 0 ? DateTimeOffset.Now.AddMinutes(cacheItemArgs.ExpireTimeOutMinutes) : (DateTimeOffset?)null, cacheItemArgs.CacheTimeOutMinutes > 0 ? TimeSpan.FromMinutes(cacheItemArgs.CacheTimeOutMinutes) : (TimeSpan?)null, cacheItemArgs.Level, cacheItemArgs.CacheDependency, cacheItemArgs.CacheCallback); // check if the item was actually saved in the cache if (GetCache <TObject>(cacheItemArgs.CacheKey, cacheItemArgs.Scope, cacheItemArgs.Level) == null) { // log the event if the item was not saved in the cache ( likely because we are out of memory ) _logger.LogError("CACHE_OVERFLOW:" + cacheItemArgs.CacheKey + ":Overflow - Item Not Cached"); } } //This thread won so remove unique Lock from collection UniqueObject.RemoveUniqueObject(cacheItemArgs.CacheKey); } } } return(objObject); }
public T GetCachedObject <T>(CacheItemArgs cacheItemArgs, CacheItemExpiredCallback cacheItemExpired) { return(CBO.GetCachedObject <T>(cacheItemArgs, cacheItemExpired)); }
internal static TObject GetCachedData <TObject>(CacheItemArgs cacheItemArgs, CacheItemExpiredCallback cacheItemExpired, bool storeInDictionary) { //declare local object and try and retrieve item from the cache object objObject = null; if (!storeInDictionary) { objObject = GetCache(cacheItemArgs.CacheKey); } else if (dictionaryCache.ContainsKey(cacheItemArgs.CacheKey)) { objObject = dictionaryCache[cacheItemArgs.CacheKey]; } if (objObject == null) { object @lock = GetUniqueLockObject(cacheItemArgs.CacheKey); lock (@lock) { if (!storeInDictionary) { objObject = GetCache(cacheItemArgs.CacheKey); } else if (dictionaryCache.ContainsKey(cacheItemArgs.CacheKey)) { objObject = dictionaryCache[cacheItemArgs.CacheKey]; } if (objObject == null) { try { objObject = cacheItemExpired(cacheItemArgs); } catch (Exception ex) { objObject = null; Exceptions.LogException(ex); } if (storeInDictionary) { dictionaryCache[cacheItemArgs.CacheKey] = objObject; } else { // set cache timeout int timeOut = cacheItemArgs.CacheTimeOut * Convert.ToInt32(Host.PerformanceSetting); if (objObject != null && timeOut > 0) { //DataCache.SetCache(cacheItemArgs.CacheKey, objObject, cacheItemArgs.CacheDependency, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(timeOut), cacheItemArgs.CachePriority, cacheItemArgs.CacheCallback); //if (DataCache.GetCache(cacheItemArgs.CacheKey) == null) //{ // LogInfo objEventLogInfo = new LogInfo(); // objEventLogInfo.LogTypeKey = CommonLibrary.Services.Log.EventLog.EventLogController.EventLogType.CACHE_OVERFLOW.ToString(); // objEventLogInfo.LogProperties.Add(new LogDetailInfo(cacheItemArgs.CacheKey, "Overflow - Item Not Cached")); // EventLogController objEventLog = new EventLogController(); // objEventLog.AddLog(objEventLogInfo); //} } } RemoveUniqueLockObject(cacheItemArgs.CacheKey); } } } if (objObject == null) { return(default(TObject)); } else { return((TObject)objObject); } }
public static TObject GetCachedData <TObject>(CacheItemArgs cacheItemArgs, CacheItemExpiredCallback cacheItemExpired) { return(GetCachedData <TObject>(cacheItemArgs, cacheItemExpired, false)); }