Ejemplo n.º 1
0
        /// <summary>
        /// Get entity from redis, but not surported mutil key of entity.
        /// </summary>
        /// <param name="personalId"></param>
        /// <param name="types"></param>
        /// <returns></returns>
        public static object[] Load(string personalId, params Type[] types)
        {
            personalId = AbstractEntity.EncodeKeyCode(personalId);
            var param = types.Where(t => !CacheFactory.ContainEntityKey(t, personalId)).ToArray();

            if (param.Length == 0)
            {
                return(null);
            }

            var result = RedisConnectionPool.GetAllEntity(personalId, param);

            foreach (var t in result)
            {
                var entity = t as AbstractEntity;
                if (entity == null)
                {
                    continue;
                }
                CacheItemSet itemSet;
                if (CacheFactory.AddOrUpdateEntity(entity, out itemSet))
                {
                    itemSet.OnLoadSuccess();
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        private IEnumerable <T> GetMutilCacheItem(IEnumerable <string> keys, bool isAutoLoad)
        {
            CheckLoad();
            //保证返回顺序
            var result    = new List <T>();
            var loadKey   = new List <string>();
            var indexList = new Queue <int>();

            foreach (var key in keys)
            {
                CacheItemSet itemSet;
                if (isAutoLoad && Container.TryGetValue(key, out itemSet) && itemSet.HasLoadSuccess)
                {
                    var itemData   = itemSet.GetItem();
                    var collection = itemData as BaseCollection;
                    if (collection == null)
                    {
                        result.Add((T)itemData);
                    }
                    else
                    {
                        result.AddRange(collection.Select(pair => (T)pair.Value));
                    }
                }
                else
                {
                    loadKey.Add(key);
                    indexList.Enqueue(result.Count);
                    result.Add(null);
                }
            }
            if (loadKey.Count == 0)
            {
                return(result);
            }
            var enumratable = RedisConnectionPool.GetAllEntity <T>(loadKey);

            foreach (var t in enumratable)
            {
                var entity = t as AbstractEntity;
                if (entity == null)
                {
                    continue;
                }
                CacheItemSet itemSet;
                if (CacheFactory.AddOrUpdateEntity(entity, out itemSet))
                {
                    itemSet.OnLoadSuccess();
                }
                int index = indexList.Count > 0 ? indexList.Dequeue() : -1;
                if (index == -1)
                {
                    result.Add(t);
                }
                else
                {
                    result[index] = t;
                }
            }
            return(result);
        }