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(); } }