Redis实战(七)

修改数据

C#语言修改Redis示例。

1、通过key修改单个value

                using (var redisClient = RedisManager.GetClient())
                {
                    var user = redisClient.GetTypedClient<User>();
                    var value = user.GetValue(txtChangeKey.Text);//首先先获取当前key的值
                    var changedUser = new User
                    {
                        Id = value.Id,
                        Name = txtChangeName.Text,
                        Job = new Job { Position = txtChangePosition.Text }
                    };//设置相应的新value值,并使其它数据与原来相统一
                    redisClient.Set(txtChangeKey.Text, changedUser);//修改value
                    value = user.GetValue(txtChangeKey.Text);//根据key获取最新的数据

                    var htmlStr = string.Empty;
                    htmlStr += "修改后的ID=" + value.Id + "&nbsp;&nbsp;姓名:" + value.Name + "&nbsp;&nbsp;所在部门:" + value.Job.Position;
                    lblPeople.Text = htmlStr;
                    lblShow.Text = "筛选后共有:1人!";

                }

2、通过keys修改多个values

            var dictionary = new Dictionary<string, User>();
            using (var redisClient = RedisManager.GetClient())
            {
                var user = redisClient.GetTypedClient<User>();
                var user1 = new User
                {
                    Id = user.GetNextSequence(),//获取新的ID
                    Name = "小明",
                    Job = new Job { Position = "Python" }
                };
                var user2 = new User
                {
                    Id = user.GetNextSequence(),
                    Name = "小红",
                    Job = new Job { Position = "Python" }
                };
                var userKeyList = user.GetAllKeys().Where(x => x.StartsWith("urn")).OrderBy(y => y).ToList();//只获取保存value的key
                dictionary.Add(userKeyList[1], user1);//第二个人
                dictionary.Add(userKeyList[2], user2);//第三个人
                redisClient.SetAll(dictionary);//同时修改多个value
               
                var users = user.GetAll();
                if (users.Count > 0)
                {
                    var htmlStr = string.Empty;
                    foreach (var u in users)
                    {
                        htmlStr += "<li>ID=" + u.Id + "&nbsp;&nbsp;姓名:" + u.Name + "&nbsp;&nbsp;所在部门:" + u.Job.Position + "</li>";
                    }
                    lblPeople.Text = htmlStr;
                }
                lblShow.Text = "筛选后共有:" + users.Count.ToString() + "人!";
            }

3、重命名key

using (var redisClient = RedisManager.GetClient())
                {
                    redisClient.RenameKey(txtKey.Text, txtNewKey.Text);
                   
                    var user = redisClient.GetTypedClient<User>();
                    var userKeyList = user.GetAllKeys();

                    if (userKeyList.Count > 0)
                    {
                        lblPeople.Text = string.Empty;
                        var htmlStr = string.Empty;
                        foreach (var u in userKeyList)
                        {
                            htmlStr += "<li>key=" + u + "</li>";
                        }
                        lblPeople.Text = htmlStr;
                    }
                    lblShow.Text = "筛选后共有:" + userKeyList.Count.ToString() + " Keys!";
                }

posted on 2016-04-26 11:40  邬兴亮  阅读(342)  评论(0编辑  收藏  举报

导航