bool IsCacheValid(ClassDefineMetadata metadata, IQueryTimestamp cacheData)
        {
            ICache dependCache = RepositoryFramework.GetCacher(metadata, "cache:region");
            var    values      = dependCache.GetBatch <long>(regions.Select(o => o.CacheKey));

            return(values.Count() == 0 || values.All(o => o < cacheData.Timestamp));
        }
        public override bool ProcessSource()
        {
            var metadata = DefineMetadata;

            Invocation.Proceed();
            ICache queryCache = RepositoryFramework.GetCacherForQuery(metadata);
            ICache cache      = RepositoryFramework.GetCacher(metadata);

            //IList接口
            TEntity item = (TEntity)Invocation.ReturnValue;

            if (item != null)
            {
                var ta       = TypeAccessor.GetAccessor(metadata.EntityType);
                var idGetter = ta.GetPropertyGetter(metadata.IdMember.Name);

                var spec = (ISpecification <TEntity>)Invocation.Arguments[0];
                if (spec == null)
                {
                    throw new ArgumentException("第一个参数类型必须为 ISpecification<TEntity>");
                }

                //加入缓存
                cache.Set(metadata.GetCacheKey(item), metadata.CloneEntity(item), 1200);
                queryCache.Set(CacheKey, new ListCacheData(new object[] { idGetter(item) })
                {
                    ShardParams = spec.ShardParams
                });
            }
            return(item != null);
        }
        public override bool ProcessSource()
        {
            var metadata = DefineMetadata;

            Invocation.Proceed();
            ICache queryCache = RepositoryFramework.GetCacherForQuery(metadata);
            ICache cache      = RepositoryFramework.GetCacher(metadata);

            //IList接口
            PagingResult <TEntity> paging = (PagingResult <TEntity>)Invocation.ReturnValue;

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

            var spec = (ISpecification <TEntity>)Invocation.Arguments[0];

            if (spec == null)
            {
                throw new ArgumentException("第一个参数类型必须为 ISpecification<TEntity>");
            }

            //加入缓存
            cache.SetBatch(paging.Items.Cast <object>().Select(o => new CacheItem <object>(metadata.GetCacheKey(o), metadata.CloneEntity(o))), 1200);
            queryCache.Set(CacheKey, new PagingCacheData(paging.Items.Select(o => idGetter(o)))
            {
                ShardParams = spec.ShardParams,
                TotalCount  = paging.TotalCount
            });
            return(true);
        }
예제 #4
0
        public static IPersister CreatePersister(IInvocation invocation, ClassDefineMetadata metadata)
        {
            DataSourcePersister  dataSource  = new DataSourcePersister(invocation, null);
            SecondLevelPersister secondLevel = new SecondLevelPersister(metadata, RepositoryFramework.GetCacher(metadata), dataSource);
            SessionPersister     session     = new SessionPersister(SessionCache.Current, secondLevel);

            return(session);
        }
        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 override bool ProcessSource()
        {
            var metadata = DefineMetadata;

            Invocation.Proceed();
            ICache cache = RepositoryFramework.GetCacher(DefineMetadata);

            object data = Invocation.ReturnValue;

            if (data != null)
            {
                cache.Set(CacheKey, new QueryCacheData()
                {
                    Data = data
                });
            }
            return(data != null);
        }
        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();
            }
        }
 public override IQueryTimestamp GetCacheData()
 {
     return(RepositoryFramework.GetCacher(DefineMetadata).Get <QueryCacheData>(CacheKey));
 }