hash又称为散列、杂凑等,是将任意长度的输入通过散列算法变换为固定长度的输出,最终输出也就是哈希值。这种转换是一种压缩映射。也就是说,散列值的空间通常要远小于输入控件,不同的输入可能会散列成相同的输出,所以不可能通过散列值来确定唯一的输入值。
哈希表hash table是为了将数据映射到数组中某个位置,通过数组下标访问元素以提高数据的查询速度,这种查询的平均期望时间复杂度为O(1)。
Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象。Redis 中每个 hash 可以存储 232 - 1 键值对(40多亿)。
Redis支持五种数据类型:String, Hash, List, Set, ZSet,其中 hash 是一个 string 类型的 field(字段) 和 value(值) 的映射表,hash 特别适合用于存储对象。hash 更适合于对象的存储,String 更适合字符串存储。
常用命令
127.0.0.1:6379[1]> HSET myhash field1 hudu # set一个具体的key-value
(integer) 1
127.0.0.1:6379[1]> HGET myhash field1
"hudu"
127.0.0.1:6379[1]> HMSET myhash field1 hello field2 world # set多个key-value
OK
127.0.0.1:6379[1]> HMGET myhash field1 field2 # 获取多个字段值
1) "hello"
2) "world"
127.0.0.1:6379[1]> HGETALL myhash # 获取全部数据
1) "field1"
2) "hello"
3) "field2"
4) "world"
127.0.0.1:6379[1]> HDEL myhash field1 # 删除hash指定的key字段,对应的value值也就消失了
(integer) 1
127.0.0.1:6379[1]> HGETALL myhash
1) "field2"
2) "world"
127.0.0.1:6379[1]> hmset myhash field1 hello field2 world
OK
127.0.0.1:6379[1]> hgetall myhash
1) "field2"
2) "world"
3) "field1"
4) "hello"
127.0.0.1:6379[1]> HLEN myhash # 获得hash表的字段数量
(integer) 2
127.0.0.1:6379[1]> HEXISTS myhash field1 # 判断hash中指定的字段是否存在
(integer) 1
127.0.0.1:6379[1]> HEXISTS myhash field3
(integer) 0
127.0.0.1:6379[1]> HKEYS myhash # 获取所有的key
1) "field2"
2) "field1"
127.0.0.1:6379[1]> HVALS myhash # 获取所有的value
1) "world"
2) "hello"
127.0.0.1:6379[1]> HSET myhash field3 5
(integer) 1
127.0.0.1:6379[1]> HINCRBY myhash field3 1 # 指定增量
(integer) 6
127.0.0.1:6379[1]> HGET myhash field3
"6"
127.0.0.1:6379[1]> HINCRBY myhash field -1
(integer) -1
127.0.0.1:6379[1]> HGET myhash field
"-1"
127.0.0.1:6379[1]> HSETNX myhash field4 hello # 不过不存在则可以设置
(integer) 1
127.0.0.1:6379[1]> HSETNX myhash field4 world
(integer) 0
信息存入redis,用表明:id作为key,用户属性作为值:
hset user:1 name 啊啊啊啊啊 age 18
代码
//Redis读取
var list = new List<InsAuthenticationInfo>();
Dictionary<string, string> cache;
using (var redisClient = RedisManager.GetReadOnlyClient())
{
cache = redisClient.GetAllEntriesFromHash(CacheKeys.InsAuthenticationMap);
}
foreach (var item in cache)
{
if (string.IsNullOrWhiteSpace(item.Key) || item.Value == null)
continue;
try
{
var info = JsonHelper.GetObject<InsAuthenticationInfo>(item.Value);
if (info != null)
list.Add(info);
}
catch (Exception e)
{
Logger.Error(e, $"解析InsAuthenticationInfo失败,JSON:{item.Value}");
}
}
if (ListIsNull(list))
return null;
return list.OrderBy(x => x.No, noComparer).ToList();
//redis修改
using (var redisClient = RedisManager.GetClient())
{
string infoStr = redisClient.GetValueFromHash(CacheKeys.InsAuthenticationMap, info.No);
if (string.IsNullOrWhiteSpace(infoStr))
throw new Exception($"信息不存在,No:{info.No}");
try
{
var updateItem = JsonHelper.GetObject<InsAuthenticationInfo>(infoStr);
updateItem.Mid = info.Mid;
updateItem.SessionId = info.SessionId;
updateItem.CanBeUse = info.CanBeUse;
updateItem.UpdateTime = DateTime.Now;
redisClient.SetEntryInHash(CacheKeys.InsAuthenticationMap, info.No, JsonHelper.GetJson(updateItem));
}
catch (Exception e)
{
Logger.Error(e, $"解析InsAuthenticationInfo失败,JSON:{infoStr}");
}
}
```