Ejemplo n.º 1
0
        public void Intercept(IInvocation invocation)
        {
            var  methodName = invocation.Method.Name;
            bool skip       = methodName == "OpenSession" || methodName == "GetShardParams" || methodName == "CreateSpecification";

            if (!skip && (methodName == "GetList" || methodName == "Get"))
            {
                var arguments = invocation.Method.GetParameters();
                skip = arguments.Length != 2 || arguments[0].ParameterType != typeof(ShardParams);
            }
            if (!skip)
            {
                ProfilerContext.BeginWatch(invocation.TargetType.FullName + "." + invocation.Method.Name);
            }

            try
            {
                var entityType = ReflectionHelper.GetEntityTypeFromRepositoryType(invocation.TargetType);
                var metadata   = RepositoryFramework.GetDefineMetadata(entityType);

                if (metadata != null)
                {
                    var mi = invocations.FirstOrDefault(o => o.IsMatch(invocation));
                    if (mi != null)
                    {
                        mi.Process(invocation, metadata);
                        ProxyEntity(invocation);
                        return;
                    }
                }
                if (skip)
                {
                    invocation.Proceed();
                }
                else
                {
                    using (MonitorContext.Repository(invocation.Method))
                        invocation.Proceed();
                }

                ProxyEntity(invocation);
            }
            finally
            {
                if (!skip)
                {
                    ProfilerContext.EndWatch();
                }
            }
        }
        public void Intercept(IInvocation invocation)
        {
            var entityType = ReflectionHelper.GetEntityTypeFromRepositoryType(invocation.TargetType);
            var metadata   = RepositoryFramework.GetDefineMetadata(entityType);

            if (metadata == null)
            {
                throw new NotSupportedException(String.Format("类型 {0} 未进行定义,因此无法实现仓储扩展。", entityType.FullName));
            }

            foreach (var type in providers)
            {
                AbstractCacheableRepositoryProvider <T> provider = (AbstractCacheableRepositoryProvider <T>)Projects.Tool.Reflection.FastActivator.Create(type);
                provider.Invocation = invocation;
                if (provider.IsMatch())
                {
                    provider.CacheKey = cacheKey;
                    var regionStr = String.Join(",", regions.Select(o => o.CacheKey));

                    var cacheData = provider.GetCacheData();
                    if (cacheData != null && IsCacheValid(metadata, cacheData))
                    {
                        log.DebugFormat("{0}.{1} query cache hit. #{2}", invocation.Method.DeclaringType.ToPrettyString(), invocation.Method.Name, provider.CacheKey);
                        if (ProfilerContext.Current.Enabled)
                        {
                            ProfilerContext.Current.Trace("platform", String.Format("hit  {1}\r\n{0}", ProfilerUtil.JsonFormat(provider.CacheKey), regionStr));
                        }

                        //处理缓存
                        using (MonitorContext.Repository(invocation.Method, true))
                            provider.ProcessCache(cacheData);
                        return;
                    }

                    //处理原始
                    var hasSource = provider.ProcessSource();

                    log.DebugFormat("{0}.{1} query cache missing. #{2}", invocation.Method.DeclaringType.ToPrettyString(), invocation.Method.Name, provider.CacheKey);
                    if (ProfilerContext.Current.Enabled)
                    {
                        ProfilerContext.Current.Trace("platform", String.Format("missing  {1}{2}\r\n{0}", ProfilerUtil.JsonFormat(provider.CacheKey), regionStr, hasSource ? "" : " for null"));
                    }
                    return;
                }
            }

            invocation.Proceed();
        }
Ejemplo n.º 3
0
        public void Process(IInvocation invocation, ClassDefineMetadata metadata)
        {
            if (metadata == null)
            {
                throw new ArgumentNullException("metadata");
            }

            var entity = invocation.Arguments[0];

            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            var interceptors = RepositoryFramework.GetInterceptors(metadata.EntityType);

            //invoke pre event
            var preUr  = new UniqueRaise(GetRaiseType(invocation, true), metadata, entity, false);
            var postUr = new UniqueRaise(GetRaiseType(invocation, false), metadata, entity, false);

            //invoke cascade delete or update
            var preRaiseType = GetRaiseType(invocation, true);

            if (preRaiseType == RaiseType.PreDelete || preRaiseType == RaiseType.PreUpdate)
            {
                Cascade(invocation, entity);
            }

            RepositoryFramework.Raise(entity, preRaiseType, preUr);

            using (MonitorContext.Repository(invocation.Method))
                invocation.Proceed();

            //invoke post event
            var postRaiseType = GetRaiseType(invocation, false);

            RepositoryFramework.Raise(entity, postRaiseType, postUr);

            //invoke cascade create
            if (postRaiseType == RaiseType.PostCreate)
            {
                Cascade(invocation, entity);
            }

            preUr.Dispose();
            postUr.Dispose();
        }
        public void Process(IInvocation invocation, ClassDefineMetadata metadata)
        {
            if (metadata == null)
            {
                throw new ArgumentNullException("metadata");
            }

            if (metadata.IsCacheable)
            {
                var    cacheKey = metadata.GetCacheKeyById(invocation.Arguments[1]);
                ICache cache    = RepositoryFramework.GetCacher(metadata);
                //CacheData value = cache.Get(cacheKey);
                var value = cache.Get(metadata.EntityType, cacheKey);
                if (value == null)
                {
                    // 调用源接口,并将结果写入缓存
                    using (MonitorContext.Repository(invocation.Method))
                        invocation.Proceed();

                    var entity = invocation.ReturnValue;
                    if (entity != null)
                    {
                        cache.Set(cacheKey, metadata.CloneEntity(entity));
                    }

                    if (ProfilerContext.Current.Enabled)
                    {
                        ProfilerContext.Current.Trace("platform", String.Format("missing get{1}\r\n{0}", cacheKey, entity == null ? " for null" : ""));
                    }
                }
                else
                {
                    //if (ProfilerContext.Current.Enabled)
                    //    ProfilerContext.Current.Trace("platform", String.Format("hit get\r\n{0}", cacheKey));
                    //invocation.ReturnValue = value.Convert(metadata.EntityType);
                    using (MonitorContext.Repository(invocation.Method, true))
                    {
                    }
                    invocation.ReturnValue = value;
                }
            }
            else
            {
                using (MonitorContext.Repository(invocation.Method))
                    invocation.Proceed();
            }
        }
        public void Process(IInvocation invocation, ClassDefineMetadata metadata)
        {
            if (metadata == null)
            {
                throw new ArgumentNullException("metadata");
            }

            if (metadata.IsCacheable)
            {
                IEnumerable ids = (IEnumerable)invocation.Arguments[1];

                var cache = RepositoryFramework.GetCacher(metadata);

                var idKeys = ids.Cast <object>().Distinct().Select(o => new IdKey(o, metadata.GetCacheKeyById(o)));

                //访问缓存并获取不在缓存中的 CacheKey
                IEnumerable <string> missing;
                //var cacheItems = cache.GetBatch<CacheData>(idKeys.Select(o => o.Key), out missing).Select(o => o.Convert(metadata.EntityType)).ToList();
                var cacheItems = cache.GetBatch(metadata.EntityType, idKeys.Select(o => o.Key), out missing).Cast <object>().ToList();

                if (missing.Count() > 0)
                {
                    var missIds = missing.Select(o => idKeys.First(ik => ik.Key == o).Id);
                    invocation.SetArgumentValue(1, missIds);

                    using (MonitorContext.Repository(invocation.Method))
                        invocation.Proceed();

                    var sourceItems = ((IEnumerable)invocation.ReturnValue).Cast <object>();

                    cacheItems.AddRange(sourceItems);

                    if (sourceItems.Count() > 0)
                    {
                        //加入缓存
                        cache.SetBatch(sourceItems.Select(o => new CacheItem <object>(metadata.GetCacheKey(o), metadata.CloneEntity(o))), 1200);
                    }
                    else
                    {
                        if (ProfilerContext.Current.Enabled)
                        {
                            ProfilerContext.Current.Trace("platform", String.Format("missing get list for null\r\n{0}", String.Join(",", missing)));
                        }
                    }
                }
                else
                {
                    using (MonitorContext.Repository(invocation.Method, true))
                    {
                    }
                }

                var ta       = TypeAccessor.GetAccessor(metadata.EntityType);
                var idGetter = ta.GetPropertyGetter(metadata.IdMember.Name);

                //按ID排序并进行类型转化
                invocation.ReturnValue = cacheItems
                                         .OrderBy(entity => idGetter(entity), ids.Cast <object>())
                                         .Cast(metadata.EntityType);

                //按ID排序并进行类型转化
                invocation.ReturnValue = cacheItems
                                         .OrderBy(entity => idGetter(entity), ids.Cast <object>())
                                         .Cast(metadata.EntityType);
            }
            else
            {
                using (MonitorContext.Repository(invocation.Method))
                    invocation.Proceed();
            }
        }