(精华)2020年8月15日 redis数据库 StackExchange.Redis中Hash类型(C#版)

RedisHashService 类请参考第一篇的封装

redis 数据结构Hash的使用

using (RedisHashService service = new RedisHashService())
{
service.SetEntryInHash("student", "id", "001");
service.SetEntryInHash("student", "name", "张小徐");
service.SetEntryInHash("student", "remark", "啦啦啦");

var keys = service.GetHashKeys("student");
var values = service.GetHashValues("student");
var keyValues = service.GetAllEntriesFromHash("student");
Console.WriteLine(service.GetValueFromHash("student", "id"));

service.SetEntryInHashIfNotExists("student", "name", "太子爷");
service.SetEntryInHashIfNotExists("student", "description", "哈哈哈");

Console.WriteLine(service.GetValueFromHash("student", "name"));
Console.WriteLine(service.GetValueFromHash("student", "description"));
service.RemoveEntryFromHash("student", "description");
Console.WriteLine(service.GetValueFromHash("student", "description"));

案例

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);
}

扩展版

using (RedisHashService service = new RedisHashService())
                {
                    service.KeyFulsh();
                    service.SetEntryInHash("lisi", "id", "15");

                    service.SetEntryInHash("zhangsan", "id", "13");
                    service.SetEntryInHash("zhangsan", "Name", "Thirteen");
                    service.SetEntryInHashIfNotExists("zhangsan", "Remark", "1234567");
                    var value13 = service.GetHashValues("zhangsan");
                    var key13 = service.GetHashKeys("zhangsan");

                    var dicList = service.GetAllEntriesFromHash("zhangsan");

                    service.SetEntryInHash("zhangsan", "id", "14");//同一条数据,覆盖
                    service.SetEntryInHash("zhangsan", "Name", "Fourteen");
                    service.SetEntryInHashIfNotExists("zhangsan", "Remark", "2345678");//同一条数据,不覆盖
                    service.SetEntryInHashIfNotExists("zhangsan", "Other", "234543");//没有数据就添加
                    service.SetEntryInHashIfNotExists("zhangsan", "OtherField", "1235665");


                    var value14 = service.GetHashValues("zhangsan");
                    service.RemoveEntryFromHash("zhangsan", "Remark");
                    service.SetEntryInHashIfNotExists("zhangsan", "Remark", "2345678");
                    value14 = service.GetHashValues("zhangsan");

                    service.StoreAsHash<Student>(student_1);
                    Student student1 = service.GetFromHash<Student>(11);
                    service.StoreAsHash<Student>(student_2);
                    Student student2 = service.GetFromHash<Student>(12);
                }
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 猿与汪的秘密 设计师:白松林 返回首页