redis实战课题
redis实战课题
本课题主要围绕用户登录时,直接到redis中查找用户的登录信息,密码验证、重置密码、查询所有用户、显示登录次数最多的前3位
,最后通过(C# SQL)持久化到SQL Server 数据库中。
1、环境搭建
2、介绍客户端API
3、保存【初始化数据到redis】
4、登录【发送登录验证码,有效时间10分钟】
5、重置密码
6、显示登录次数最多的3位用户
7、持久化数据到SQL server 数据库中。
代码块如下
public List<UserInfo> GetData() { //为简便,这里使用穷举例,不从DB中取数 List<UserInfo> list = new List<UserInfo>() { new UserInfo(){Id=1001,Name="李雷1001",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1002,Name="李雷1002",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1003,Name="李雷1003",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1004,Name="李雷1004",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1005,Name="李雷1005",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1006,Name="李雷1006",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1007,Name="李雷1007",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1008,Name="李雷1008",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1009,Name="李雷1009",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1010,Name="李雷1010",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1011,Name="寒梅1011",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1012,Name="寒梅1012",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1013,Name="寒梅1013",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1014,Name="寒梅1014",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1015,Name="寒梅1015",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1016,Name="寒梅1016",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1017,Name="寒梅1017",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1018,Name="寒梅1018",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1019,Name="寒梅1019",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1020,Name="寒梅1020",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1021,Name="寒梅1021",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1022,Name="寒梅1022",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1023,Name="寒梅1023",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1024,Name="寒梅1024",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1025,Name="寒梅1025",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1026,Name="寒梅1026",age=101,Pwd="1234",sex="男",times=DateTime.Now }, new UserInfo(){Id=1027,Name="寒梅1027",age=101,Pwd="1234",sex="男",times=DateTime.Now }, }; return list; } public void InitRedisData() { List<UserInfo> list = GetData(); using (IRedisClient RClient = RedisManager.GetRedisClient()) { IRedisTypedClient<UserInfo> IRUserInfo = RClient.As<UserInfo>(); IRUserInfo.StoreAll(list); //foreach (var item in list) //{ // RClient.StoreAsHash<UserInfo>(item); //} } } public void ResetPwd(string id, string pwd, string newpwd) { using (IRedisClient RClient = RedisManager.GetRedisClient()) { IRedisTypedClient<UserInfo> IRUserinfo = RClient.As<UserInfo>(); UserInfo userinfo = IRUserinfo.GetValue("urn:userinfo:" + id); userinfo.Pwd = newpwd; IRUserinfo.SetValue("urn:userinfo:" + id, userinfo); } } public List<UserInfo> GetRedisData() { List<UserInfo> list = new List<UserInfo>(); using (IRedisClient RClient = RedisManager.GetRedisClient()) { IRedisTypedClient<UserInfo> IRUserInfo = RClient.As<UserInfo>(); list = (List<UserInfo>)IRUserInfo.GetAll().OrderByDescending(i => i.times).OrderByDescending(i => i.Name).Take(5).ToList(); } return list; }