Exemplo n.º 1
0
        /// <summary>
        /// 通过Redis键获取实体对象
        /// </summary>
        /// <param name="key"></param>
        /// <param name="isRemove"></param>
        /// <param name="type"></param>
        /// <param name="serializer"></param>
        /// <returns></returns>
        public static dynamic GetEntityFromRedis(string key, bool isRemove, Type type, ICacheSerializer serializer)
        {
            string  typeName;
            string  asmName;
            bool    isEntityType;
            string  redisKey;
            string  entityKey = GetEntityTypeFromKey(key, out typeName, ref type, out asmName, out isEntityType, out redisKey);
            dynamic entity    = null;

            RedisConnectionPool.Process(client =>
            {
                if (isEntityType)
                {
                    var data = client.Get <byte[]>(redisKey);
                    if (data != null && type != null)
                    {
                        entity = serializer.Deserialize(data, type);
                    }
                }
                else
                {
                    var data = client.Get <byte[]>(redisKey);
                    if (data != null && type != null)
                    {
                        var dict = (IDictionary)serializer.Deserialize(data, type);
                        entity   = dict[entityKey];
                    }
                }
                if (entity == null)
                {
                    //新版本Hash格式
                    var data = client.HGet(typeName, RedisConnectionPool.ToByteKey(entityKey));
                    if (data != null && type != null)
                    {
                        entity = serializer.Deserialize(data, type);
                    }
                }

                if (isRemove && entity == null && type != null)
                {
                    //临时队列删除Entity
                    string setId     = (isEntityType ? RedisConnectionPool.EncodeTypeName(typeName) : redisKey) + ":remove";
                    IDictionary dict = null;
                    RedisConnectionPool.ProcessTrans(client, new string[] { setId }, () =>
                    {
                        var data = client.Get <byte[]>(setId);
                        if (data == null)
                        {
                            return(false);
                        }
                        dict   = (IDictionary)serializer.Deserialize(data, type);
                        entity = dict[entityKey];
                        dict.Remove(entityKey);

                        return(true);
                    }, trans =>
                    {
                        if (dict != null && dict.Count > 0)
                        {
                            trans.QueueCommand(c => c.Set(setId, serializer.Serialize(dict)));
                        }
                        else
                        {
                            trans.QueueCommand(c => c.Remove(setId));
                        }
                    }, null);
                }
            });
            return(entity);
        }