Exemple #1
0
        /// <summary>
        /// 新增用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool AddUserInfo(T_Sys_User user, out string msg)
        {
            msg = string.Empty;
            //检验该账户是否已存在
            var existUser = Db.Select<T_Sys_User>()
                .Where(i => i.Account.Equals(user.Account, StringComparison.CurrentCultureIgnoreCase)).First();
            if (existUser != null)
            {
                msg = "该账号已存在!";
                return false;
            }

            //密码MD5加密
            user.Password = EncryptHelper.MD5Encrypt(user.Password);
            user.CreateTime = DateTime.Now;
            user.UpdateTime = DateTime.Now;
            long id = Db.Insert<T_Sys_User>(user).ExecuteIdentity();

            if (id <= 0)
            {
                msg = "新增用户失败,请联系系统管理员!";
                return false;
            }

            //写入缓存
            using (RedisHashService service = new RedisHashService())
            {
                user.Id = id;
                service.StoreAsHash(user);
            }

            return true;
        }
Exemple #2
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 #3
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);
            }
        }