示例#1
0
        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) || this.cache == null)
                {
                    return(getNext()(input, getNext));
                }

                var inputs = new object[input.Inputs.Count];

                for (var i = 0; i < inputs.Length; ++i)
                {
                    inputs[i] = input.Inputs[i];
                }

                var cacheKey     = keyGenerator.CreateCacheKey(input.MethodBase, inputs);
                var cachedResult = getCachedResult(cacheKey);

                if (cachedResult == null)
                {
                    var realReturn = getNext()(input, getNext);
                    if (realReturn.Exception == null && realReturn.ReturnValue != null)
                    {
                        AddToCache(cacheKey, realReturn.ReturnValue);
                    }
                    return(realReturn);
                }

                var cachedReturn = input.CreateMethodReturn(cachedResult, input.Arguments);
                return(cachedReturn);
            }
        }
        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);

                IMethodReturn returnResult = null;

                returnResult = TryToInvoke(cacheKey, returnResult);

                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 Gets PessimisticCacheEntryStatus

                IMethodReturn returnResult = null;

                if (cacheEntryStatus.IsOperationFaulted || !cacheManager.Contains(cacheKey))
                {
                    returnResult = TryToInvoke(cacheKey, cacheEntryStatus, returnResult);
                }
                else
                {
                    returnResult = GetMethodReturnFromCache(cacheKey, input.Arguments);
                }
                return(returnResult);
            }
        }
        public static void UpdateCache <T>(Object updatedValue, String methodName, Object[] input = null)
        {
            input = input != null ? input : new object[] { };
            var    method       = typeof(T).GetMethod(methodName);
            var    keyGenerator = new DefaultCacheKeyGenerator();
            var    cache        = EnterpriseLibraryContainer.Current.GetInstance <ICacheManager>();
            string cacheKey     = keyGenerator.CreateCacheKey(method, input);

            if (updatedValue == null && cache.Contains(cacheKey))
            {
                cache.Remove(cacheKey);
            }
            else
            {
                cache.Add(cacheKey, updatedValue);
            }
        }
示例#5
0
        private IMethodReturn loadUsingCache()
        {
            //We need to synchronize calls to the FastCacheHandler on method level
            //to prevent duplicate calls to methods that could be cached.
            lock (input.MethodBase)
            {
                #region void methods are executed

                if (TargetMethodReturnsVoid(input) || this.cache == null)
                {
                    return(getNext()(input, getNext));
                }

                #endregion void methods are executed

                #region Computes cacheKey

                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);

                #endregion Computes cacheKey

                object cachedResult = getCachedResult(cacheKey);
                if (cachedResult == null)
                {
                    return(InvokeAndCache(cacheKey));
                }
                else
                {
                    // Computes the response based on cache content
                    var cachedReturn = input.CreateMethodReturn(cachedResult, input.Arguments);
                    // In a new Thread requests the new cache content.
                    System.Threading.Tasks.Task.Factory.StartNew <IMethodReturn>(InvokeAndCache, cacheKey);
                    // Returns the cache Content
                    return(cachedReturn);
                }
            }
        }