Example #1
0
        private async Task Invoke(ICacheProvider cacheProvider, InterceptMethodAttribute attribute, string key, IInvocation invocation)
        {
            switch (attribute.Method)
            {
            case CachingMethod.Get:
            {
                var retrunValue = await cacheProvider.GetFromCacheFirst(key, async() =>
                    {
                        await invocation.Proceed();
                        return(invocation.ReturnValue);
                    }, invocation.ReturnType, attribute.Time);

                invocation.ReturnValue = retrunValue;
                break;
            }

            default:
            {
                await invocation.Proceed();

                var keys = attribute.CorrespondingKeys.Select(correspondingKey => string.Format(correspondingKey, key)).ToList();
                keys.ForEach(cacheProvider.RemoveAsync);
                break;
            }
            }
        }
        private async Task CacheIntercept(InterceptMethodAttribute attribute, string key, ICacheInvocation invocation, string l2Key, bool enableL2Cache)
        {
            ICacheProvider cacheProvider = null;

            switch (attribute.Mode)
            {
            case CacheTargetType.Redis:
            {
                cacheProvider = CacheContainer.GetService <ICacheProvider>(string.Format("{0}.{1}",
                                                                                         attribute.CacheSectionType.ToString(), CacheTargetType.Redis.ToString()));
                break;
            }

            case CacheTargetType.MemoryCache:
            {
                cacheProvider = CacheContainer.GetService <ICacheProvider>(CacheTargetType.MemoryCache.ToString());
                break;
            }
            }
            if (cacheProvider != null && !enableL2Cache)
            {
                await Invoke(cacheProvider, attribute, key, invocation);
            }
            else if (cacheProvider != null && enableL2Cache)
            {
                var l2CacheProvider = CacheContainer.GetService <ICacheProvider>(CacheTargetType.MemoryCache.ToString());
                if (l2CacheProvider != null)
                {
                    await Invoke(cacheProvider, l2CacheProvider, l2Key, attribute, key, invocation);
                }
            }
        }
Example #3
0
        private async Task CacheIntercept(InterceptMethodAttribute attribute, string key, IInvocation invocation)
        {
            ICacheProvider cacheProvider = null;

            switch (attribute.Mode)
            {
            case CacheTargetType.Redis:
            {
                cacheProvider = CacheContainer.GetInstances <ICacheProvider>(string.Format("{0}.{1}",
                                                                                           attribute.CacheSectionType.ToString(), CacheTargetType.Redis.ToString()));
                break;
            }

            case CacheTargetType.MemoryCache:
            {
                cacheProvider = CacheContainer.GetInstances <ICacheProvider>(CacheTargetType.MemoryCache.ToString());
                break;
            }
            }
            if (cacheProvider != null)
            {
                await Invoke(cacheProvider, attribute, key, invocation);
            }
        }
        /// <summary>
        /// 拦截执行的方法
        /// </summary>
        /// <param name="invocation">拦截的对象</param>
        public void Intercept(IInvocation invocation)
        {
            var keyIndex   = default(object);
            var attributes = invocation.Method.GetCustomAttributes(false);
            InterceptMethodAttribute methodAttribute = default(InterceptMethodAttribute);
            LoggerInterceptAttribute loggerAttr      = default(LoggerInterceptAttribute);

            foreach (var attribute in attributes)
            {
                if (attribute is InterceptMethodAttribute)
                {
                    methodAttribute = (InterceptMethodAttribute)attribute;
                }
                else if (attribute is LoggerInterceptAttribute)
                {
                    loggerAttr = (LoggerInterceptAttribute)attribute;
                }
            }

            var paramInfos = invocation.Method.GetParameters();

            if (invocation.Arguments.Length > 0)
            {
                keyIndex = invocation.GetArgumentValue(0);
            }
            if (paramInfos.Count() > 0)
            {
                var param = invocation.GetArgumentValue(0);
                if (!(param is IEnumerable))
                {
                    var props = param.GetType().GetTypeInfo().GetProperties();
                    foreach (var prop in props)
                    {
                        var propattr = prop.GetCustomAttributes(false);

                        object attr =
                            (from row in propattr where row.GetType() == typeof(CacheKeyAttribute) select row).
                            FirstOrDefault();
                        if (attr == null)
                        {
                            continue;
                        }

                        keyIndex = prop.GetValue(param, null);
                    }
                }
            }

            #region 缓存处理

            if (methodAttribute != null)
            {
                var keys = new List <string>();

                var key = methodAttribute.Key;
                key = string.IsNullOrEmpty(key)
                          ? string.Format(BASEKEY, invocation.Method.Name, keyIndex)
                          : string.Format(key, keyIndex);
                if (methodAttribute.Method != CachingMethod.Get)
                {
                    var correspondingKeys = methodAttribute.CorrespondingKeys;
                    keys.AddRange(correspondingKeys.Select(correspondingKey => string.Format(correspondingKey, keyIndex)));
                }
                switch (methodAttribute.Mode)
                {
                case CacheTargetType.MemoryCache:
                {
                    CacheIntercept(methodAttribute.Method, invocation, key, methodAttribute.CacheSectionType, methodAttribute.Time, keys);
                    break;
                }

                case CacheTargetType.CouchBase:
                {
                    //CouchBaseIntercept(methodAttribute.Method, invocation, key, methodAttribute.Time,
                    //                   methodAttribute.CacheSectionType, keys);
                    break;
                }

                case CacheTargetType.Redis:
                {
                    RedisIntercept(methodAttribute.Method, invocation, key, methodAttribute.CacheSectionType, methodAttribute.Time);
                    break;
                }
                }
            }
            else
            {
                invocation.Proceed();
            }

            #endregion

            invocation.ReturnValue = invocation.ReturnValue;
        }