Exemplo n.º 1
0
        public void Intercept(IInvocation invocation)
        {
            var method = invocation.Method;
            var key    = CacheUtility.GetCacheName(invocation.MethodInvocationTarget.ReflectedType);
            //var config =
            //    CacheConfiguration.Instance.CacheConfigurations.FirstOrDefault(
            //        a => TypeUtils.GetFormattedName(a.Name).Equals(key));

            var config = _cacheConfiguration.Instance.CacheConfigurations.FirstOrDefault(a => TypeUtils.GetFormattedName(a.Name).Equals(key));

            if (method.IsDefined(typeof(CachingAttribute), true) && config != null)
            {
                var cachingAttribute =
                    (CachingAttribute)method.GetCustomAttributes(typeof(CachingAttribute), true)[0];
                var cache = _cacheFactory.GetCache(carrierCode, key);
                switch (cachingAttribute.Method)
                {
                case CachingMethod.Get:
                {
                    //全局和实体都为true,才走ignite
                    //if (CacheConfiguration.Instance.UseFullCache && config.AllCache)
                    if (_cacheConfiguration.Instance.UseFullCache && config.AllCache)
                    {
                        cache = _cacheFactory.GetCache(carrierCode, invocation.TargetType.GenericTypeArguments[0]);
                        var arg = invocation.Arguments.Length > 0 ? invocation.Arguments[0] : null;
                        //此处只支持表达式的解析,类似Get(PK)的方法会绕过,从DbContext走EF的内部机制
                        if ((arg != null) && (arg.GetType().BaseType?.Name == "LambdaExpression"))
                        {
                            var cacheType = cache.GetType();
                            var getMethod = GetMethod(cacheType, "GetByExpression", true,
                                                      invocation.TargetType.GenericTypeArguments[0]);
                            var result = getMethod.MakeGenericMethod(invocation.TargetType.GenericTypeArguments[0])
                                         .Invoke(
                                cache, new[]
                                {
                                    arg
                                });
                            //缓存没数据
                            if (result == null || ((IList)result).Count == 0)
                            {
                                invocation.Proceed();
                            }
                            else
                            {
                                //有可能是first查询
                                if (invocation.GetConcreteMethod().ReturnType.Name.Contains("IQueryable"))
                                {
                                    invocation.ReturnValue = ((IList)result).AsQueryable();
                                }
                                else
                                {
                                    invocation.ReturnValue = ((IList)result).Count > 0 ? ((IList)result)[0] : null;
                                }
                            }
                        }
                        else
                        {
                            invocation.Proceed();
                        }
                    }
                    else
                    {
                        var valKey = GetValueKey(cachingAttribute, invocation);
                        if (cache.Contains(valKey))
                        {
                            var obj = cache.Get(valKey);
                            invocation.ReturnValue =
                                invocation.GetConcreteMethod().ReturnType.Name.Contains("IQueryable")
                                            ? ((IList)obj).AsQueryable()
                                            : obj;
                        }
                        else
                        {
                            invocation.Proceed();
                            var val = invocation.ReturnValue;
                            if (val is IQueryable)
                            {
                                //转换为list,queryable无法序列化
                                var q           = val as IQueryable;
                                var genericType = typeof(List <>).MakeGenericType(q.ElementType);
                                var constructor =
                                    genericType.GetDeclaredConstructors().Skip(2).Take(1).FirstOrDefault();
                                val = constructor?.Invoke(new object[] { q }) as IList;
                            }
                            if (val != null)
                            {
                                if (invocation.ReturnValue is IQueryable && !(val is IQueryable))
                                {
                                    invocation.ReturnValue = (val as IList).AsQueryable();
                                }
                                else
                                {
                                    invocation.ReturnValue = val;
                                }
                                cache.Add(valKey, val);
                                if (_logger.IsDebugEnabled)
                                {
                                    _logger.Debug(
                                        string.Format("CachingInterceptor Get: Key:{0} valKey:{1} addTime:{2}", key,
                                                      valKey, DateTime.Now));
                                }
                            }
                        }
                    }
                }
                break;
                //TODO 需要全缓存才有用 改ogg方式
                //case CachingMethod.Put:
                //{
                //    invocation.Proceed();
                //    if (cache.Contains(valKey))
                //    {
                //        if (cachingAttribute.Force)
                //        {
                //            cache.Remove(valKey);
                //            cache.Add(valKey, invocation.ReturnValue);
                //        }
                //        else
                //            cache.Put(valKey, invocation.ReturnValue);
                //    }
                //    else
                //        cache.Add(valKey, invocation.ReturnValue);

                //    if (_logger.IsInfoOn)
                //        _logger.Info(string.Format("CachingInterceptor Put: Key:{0} valKey:{1} addTime:{2}", key,
                //            valKey, DateTime.Now));
                //}
                //    break;
                //case CachingMethod.Remove:
                //{
                //    var removeKeys = cachingAttribute.CorrespondingMethodNames;
                //    foreach (var removeKey in removeKeys)
                //    {
                //        if (key.Equals(TypeUtils.GetFormattedName(removeKey)))
                //        {
                //            cache.Flush();
                //        }
                //        // if (cache.Contains(removeKey))
                //        //     cache.Remove(removeKey);
                //    }
                //    invocation.Proceed();

                //    if (_logger.IsInfoOn)
                //        _logger.Info(string.Format("CachingInterceptor Remove: Key:{0} valKey:{1} addTime:{2}", key,
                //            valKey, DateTime.Now));
                //}
                //    break;
                default:
                    invocation.Proceed();
                    break;
                }
            }
            else
            {
                invocation.Proceed();
            }
        }
Exemplo n.º 2
0
        public ICache GetCache <T>(string carrierCode)
        {
            var cacheName = CacheUtility.GetCacheName <T>();

            return(GetCache(carrierCode, cacheName));
        }
Exemplo n.º 3
0
        public virtual ICache GetCache(string carrierCode, Type cacheType)
        {
            var cacheName = CacheUtility.GetCacheName(cacheType);

            return(GetCache(carrierCode, cacheName));
        }