private IMethodReturn TryToInvoke(string cacheKey, PessimisticCacheEntryStatus cacheEntryStatus, IMethodReturn returnResult) { returnResult = getNext()(input, getNext); if (returnResult.Exception == null && returnResult.ReturnValue != null) { AddToCache(cacheKey, returnResult.ReturnValue); cacheEntryStatus.IsOperationFaulted = false; } else { if (cacheManager.Contains(cacheKey)) { returnResult = GetMethodReturnFromCache(cacheKey, input.Arguments); } } return(returnResult); }
private IMethodReturn loadUsingCache() { //We need to synchronize calls to the CacheHandler on method level //to prevent duplicate calls to methods that could be cached. lock (input.MethodBase) { if (TargetMethodReturnsVoid(input)) { return(getNext()(input, getNext)); } var inputs = new object[input.Inputs.Count]; for (int i = 0; i < inputs.Length; ++i) { inputs[i] = input.Inputs[i]; } string cacheKey = keyGenerator.CreateCacheKey(input.MethodBase, inputs); PessimisticCacheEntryStatus cacheEntryStatus; #region Gets PessimisticCacheEntryStatus if (!pessimisticCacheStatus.ContainsKey(cacheKey)) { cacheEntryStatus = pessimisticCacheStatus[cacheKey] = new PessimisticCacheEntryStatus() { IsOperationFaulted = true }; } else { cacheEntryStatus = pessimisticCacheStatus[cacheKey]; } #endregion IMethodReturn returnResult = null; if (cacheEntryStatus.IsOperationFaulted || !cacheManager.Contains(cacheKey)) { returnResult = TryToInvoke(cacheKey, cacheEntryStatus, returnResult); } else { returnResult = GetMethodReturnFromCache(cacheKey, input.Arguments); } return(returnResult); } }