Exemple #1
0
        public override void InterceptMethod(IInvocation invocation, MethodBase method, Attribute attribute)
        {
            CachingAttribute cachingAttribute = attribute as CachingAttribute;

            if (cachingAttribute == null)
            {
                invocation.Proceed();
            }
            else
            {
                string cacheKey     = _keyGenerator.CreateCacheKey(method, invocation.Arguments);
                object cachedResult = CacheManager.Get(cacheKey);
                if (cachedResult == null)
                {
                    lock (method)
                    {
                        cachedResult = CacheManager.Get(cacheKey);
                        if (cachedResult == null)
                        {
                            invocation.Proceed();
                            cachedResult = invocation.ReturnValue;
                            CacheManager.Set(cacheKey, cachedResult, cachingAttribute.CacheTimeSpan);
                        }
                    }
                }

                invocation.ReturnValue = cachedResult;
            }
        }
Exemple #2
0
        /// <summary>
        /// Implements the caching behavior of this handler.
        /// </summary>
        /// <param name="input"><see cref="IMethodInvocation"/> object describing the current call.</param>
        /// <param name="getNext">delegate used to get the next handler in the current pipeline.</param>
        /// <returns>Return value from target method, or cached result if previous inputs have been seen.</returns>
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            if (TargetMethodReturnsVoid(input))
            {
                return(getNext()(input, getNext));
            }

            object[] 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);

            object[] cachedResult = (object[])HttpRuntime.Cache.Get(cacheKey);

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

            IMethodReturn cachedReturn = input.CreateMethodReturn(cachedResult[0], input.Arguments);

            return(cachedReturn);
        }
        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            if (IsMethodCachable(input))
            {
                string regionName = _cacheRegionNameGenerator.CreateRegionName(
                    string.Format("{0}.{1}", input.Target.GetType().FullName, input.MethodBase.Name));

                string cacheKey = _cacheKeyGenerator.CreateCacheKey(input.Inputs);

                ICacheRegion cacheRegion = _cacheProvider.GetRegion(regionName);

                return(cacheRegion.GetOrAdd(cacheKey,
                                            () => getNext.Invoke().Invoke(input, getNext)
                                            ));
            }

            return(getNext()
                   .Invoke(input, getNext));
        }