///大key
string hashid = "xushikun";
#region 向hashid集合中添加key/value
client.SetEntryInHash(hashid, "id", "001");
Console.WriteLine(client.GetValuesFromHash(hashid, "id").FirstOrDefault());
client.SetEntryInHash(hashid, "name", "xushikun");
Console.WriteLine(client.GetValuesFromHash(hashid, "name").FirstOrDefault());
client.SetEntryInHash(hashid, "socre", "100");
Console.WriteLine(client.GetValuesFromHash(hashid, "socre").FirstOrDefault());
#endregion
#region 存储对象T t到hash集合中
//urn: 类名: id的值 、、如果使用对象操作,一定要有id
client.StoreAsHash<UserInfo>(new UserInfo() { Id = 1, Name = "clay", number = 0 });
var olduserinfo = client.GetFromHash<UserInfo>(1);
//如果id存在的话,则覆盖之前相同的id 他帮助我们序列化或者反射了一些事儿
client.StoreAsHash<UserInfo>(new UserInfo() { Id = 2, Name = "clay2" });
//获取对象T中ID为id的数据。 必须要有属性id,不区分大小写
Console.WriteLine(client.GetFromHash<UserInfo>(1).Name);
var olduserinfo = client.GetFromHash<UserInfo>(1);
olduserinfo.number = 4;
client.StoreAsHash<UserInfo>(olduserinfo);
Console.WriteLine("最后的结果" + client.GetFromHash<UserInfo>(1).number);
client.StoreAsHash<UserInfoTwo>(new UserInfoTwo() { Id = "001", Name = "clay2" });
Console.WriteLine(client.GetFromHash<UserInfoTwo>("001").Name);
client.StoreAsHash<UserInfoTwo>(new UserInfoTwo() { Id = "002", Name = "clay" });
Console.WriteLine(client.GetFromHash<UserInfoTwo>("002").Name);
UserInfo lisi = new UserInfo() { Id = 1, Name = "李四", number = 0 };
client.StoreAsHash<UserInfo>(lisi);
Console.WriteLine(client.GetFromHash<UserInfo>(1).number);
//做个自增
var oldzhang = client.GetFromHash<UserInfo>(1);
oldzhang.number++;
client.StoreAsHash<UserInfo>(oldzhang);
#endregion
#region 批量新增key的值
Dictionary<string, string> pairs = new Dictionary<string, string>();
pairs.Add("id", "001");
pairs.Add("name", "clay");
client.SetRangeInHash(hashid, pairs);
//获取当前key的值
Console.WriteLine(client.GetValueFromHash(hashid, "id"));
Console.WriteLine(client.GetValueFromHash(hashid, "name"));
//一次性的获取所有想要获取的小key(属性的)值 如果key不存在,则返回空,不抛出异常
var list = client.GetValuesFromHash(hashid, "id", "name", "abc");
Console.WriteLine("*********");
foreach (var item in list)
{
Console.WriteLine(item);
}
#endregion
redis hash的使用
于 2023-03-06 20:58:42 首次发布