/// <summary>
        /// 拦截机制
        /// </summary>
        /// <param name="inputContext"></param>
        public void Interceptor(IInvocationContext inputContext)
        {
            //获取方法
            var tempMethod = inputContext.Method;

            //获得缓存键
            var tempKey = CacheUtility.GetMethodCacheKey(inputContext);

            if (string.IsNullOrWhiteSpace(tempKey))
            {
                inputContext.Proceed();
                return;
            }
            else
            {
                //是否已执行
                bool ifProcessed = false;

                try
                {
                    var tempCache = CacheUtility.GetCache(tempKey);

                    //若没有缓存
                    if (null == tempCache || tempCache.Value.GetType() != tempMethod.ReturnType)
                    {
                        //设置执行状态
                        ifProcessed = true;
                        inputContext.Proceed();

                        if (null != inputContext.ReturnValue)
                        {
                            //保存缓存
                            CacheUtility.SetCache(tempKey, inputContext.ReturnValue);
                        }
                    }
                    else
                    {
                        //设置缓存
                        inputContext.ReturnValue = tempCache.Value;
                    }
                }
                //异常直接执行
                catch (Exception)
                {
                    //若未执行
                    if (!ifProcessed)
                    {
                        inputContext.Proceed();
                    }
                    return;
                }
            }
        }
Esempio n. 2
0
        public void Interceptor(IInvocationContext inputContext)
        {
            //获得缓存键
            var tempKey = CacheUtility.GetMethodCacheKey(inputContext);

            inputContext.Proceed();

            if (!string.IsNullOrEmpty(tempKey))
            {
                CacheUtility.DeleteCache(tempKey);
            }
        }