Exemple #1
0
        /// <summary>
        /// 根据Id查找用户信息/登录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T_Sys_User GetUserInfoById(long id)
        {
            T_Sys_User userInfo;
            using (RedisHashService service = new RedisHashService())
            {
                userInfo = service.GetFromHash<T_Sys_User>(id);
                if (userInfo?.Id == id)
                    return userInfo;
            }
            userInfo = Db.Select<T_Sys_User>().Where(i => i.Id == id).ToList().FirstOrDefault();
            //查出来再存到缓存中
            if (userInfo?.Id == id)
            {
                using (RedisHashService service = new RedisHashService())
                {
                    service.StoreAsHash(userInfo);
                }
            }

            return userInfo;
        }
Exemple #2
0
        public static void Show()
        {
            UserInfo user = new UserInfo()
            {
                Id       = 123,
                Account  = "Administrator",
                Address  = "武汉市",
                Email    = "*****@*****.**",
                Name     = "Eleven",
                Password = "******",
                QQ       = 57265177
            };


            using (RedisStringService service = new RedisStringService())
            {
                //service.Set<string>($"userinfo_{user.Id}", Newtonsoft.Json.JsonConvert.SerializeObject(user));
                service.Set <UserInfo>($"userinfo_{user.Id}", user);
                var userCacheList = service.Get <UserInfo>(new List <string>()
                {
                    $"userinfo_{user.Id}"
                });
                var userCache = userCacheList.FirstOrDefault();
                //string sResult = service.Get($"userinfo_{user.Id}");
                //var userCache = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInfo>(sResult);
                userCache.Account = "Admin";
                service.Set <UserInfo>($"userinfo_{user.Id}", userCache);
            }
            using (RedisHashService service = new RedisHashService())
            {
                service.FlushAll();
                //反射遍历做一下
                service.SetEntryInHash($"userinfo_{user.Id}", "Account", user.Account);
                service.SetEntryInHash($"userinfo_{user.Id}", "Name", user.Name);
                service.SetEntryInHash($"userinfo_{user.Id}", "Address", user.Address);
                service.SetEntryInHash($"userinfo_{user.Id}", "Email", user.Email);
                service.SetEntryInHash($"userinfo_{user.Id}", "Password", user.Password);
                service.SetEntryInHash($"userinfo_{user.Id}", "Account", "Admin");

                service.StoreAsHash <UserInfo>(user);//含ID才可以的
                var result = service.GetFromHash <UserInfo>(user.Id);
            }
            UserInfo user2 = new UserInfo()
            {
                Id       = 234,
                Account  = "Administrator2",
                Address  = "武汉市2",
                Email    = "[email protected]",
                Name     = "Eleven2",
                Password = "******",
                QQ       = 572651772
            };

            using (RedisHashService service = new RedisHashService())
            {
                service.FlushAll();
                //反射遍历做一下
                service.SetEntryInHash($"userinfo_{user2.Id}", "Account", user2.Account);
                service.SetEntryInHash($"userinfo_{user2.Id}", "Name", user2.Name);
                service.SetEntryInHash($"userinfo_{user2.Id}", "Address", user2.Address);
                service.SetEntryInHash($"userinfo_{user2.Id}", "Email", user2.Email);
                service.SetEntryInHash($"userinfo_{user2.Id}", "Remark", "这里是2号用户");

                service.StoreAsHash <UserInfo>(user);//含ID才可以的
                var result = service.GetFromHash <UserInfo>(user.Id);
            }
        }