Redis 哈希(Hash)

Redis 哈希数据类型(hash)是一个 string 类型的 field(字段) 和 value(值) 的映射表,hash 特别适合用于存储对象。

Redis 中每个 hash 可以存储 2^32 - 1 个键值对(40多亿)。

实例

(1)将用户基本信息采用哈希数据结构保存,如下:

# 保存用户数据到hash
127.0.0.1:6379> hset user:100 id 100 name Hxstrive country CN
(integer) 3

# 获取用户数据hash中的name字段
127.0.0.1:6379> hget user:100 name
"Hxstrive"

# 获取用户数据hash中的所有数据
127.0.0.1:6379> hgetall user:100
1) "id"
2) "100"
3) "name"
4) "Hxstrive"
5) "country"
6) "CN"

(2)使用 hash 作为计数器,计数设备 “777” 的ping服务器、发出请求、发送错误的次数。如下:

# 777 设备 ping 三次
127.0.0.1:6379> HINCRBY device:777:stats pings 1
(integer) 1
127.0.0.1:6379> HINCRBY device:777:stats pings 1
(integer) 2
127.0.0.1:6379> HINCRBY device:777:stats pings 1
(integer) 3

# 777 设备错误一次
127.0.0.1:6379> HINCRBY device:777:stats errors 1
(integer) 1

# 777 设备请求一次
127.0.0.1:6379> HINCRBY device:777:stats requests 1
(integer) 1

# 获取 777 设备的 ping 次数
127.0.0.1:6379> HGET device:777:stats pings
"3"

# 获取 777 设备请求和错误次数
127.0.0.1:6379> HMGET device:777:stats requests errors
1) "1"
2) "1"

Redis hash 命令

下面列出了 Redis hash 基本的相关命令。

HDEL命令

删除一个或多个哈希表字段。语法如下:

HDEL key field1 [field2]

实例:

127.0.0.1:6379> hset user:100 id 100 name Hxstrive country CN
(integer) 3
127.0.0.1:6379> hdel user:100 name country
(integer) 2
127.0.0.1:6379> hgetall user:100
1) "id"
2) "100"

HEXISTS命令

查看哈希表 key 中,指定的字段是否存在。语法如下:

HEXISTS key field

实例:

127.0.0.1:6379> hexists user:100 id
(integer) 1
127.0.0.1:6379> hexists user:100 name
(integer) 0

HGET命令

获取存储在哈希表中指定字段的值。语法如下:

HGET key field

实例:

127.0.0.1:6379> hset user:100 id 100 name Hxstrive country CN
(integer) 3
127.0.0.1:6379> hget user:100 name
"Hxstrive"

HGETALL命令

获取在哈希表中指定 key 的所有字段和值。语法如下:

HGETALL key

实例:

127.0.0.1:6379> hset user:100 id 100 name Hxstrive country CN
(integer) 3
127.0.0.1:6379> hgetall user:100
1) "id"
2) "100"
3) "name"
4) "Hxstrive"
5) "country"
6) "CN"

HINCRBY命令

为哈希表 key 中的指定字段的整数值加上增量 increment。语法如下:

HINCRBY key field increment

实例:

127.0.0.1:6379> hget user:100 id
"100"
127.0.0.1:6379> hincrby user:100 id 10
(integer) 110
127.0.0.1:6379> hget user:100 id
"110"

HINCRBYFLOAT命令

为哈希表 key 中的指定字段的浮点数值加上增量 increment。语法如下:

HINCRBYFLOAT key field increment

实例:

127.0.0.1:6379> hset user:100 id 100 name Hxstrive country CN
(integer) 3
127.0.0.1:6379> hincrbyfloat user:100 id 10.5
"110.5"

HKEYS命令

获取所有哈希表中的字段。语法如下:

HKEYS key

实例:

127.0.0.1:6379> hset user:100 id 100 name Hxstrive country CN
(integer) 3
127.0.0.1:6379> hkeys user:100
1) "id"
2) "name"
3) "country"

HLEN命令

获取哈希表中字段的数量。语法如下:

HLEN key

实例:

127.0.0.1:6379> hset user:100 id 100 name Hxstrive country CN
(integer) 3
127.0.0.1:6379> hlen user:100
(integer) 3

HMGET命令

获取所有给定字段的值。语法如下:

HMGET key field1 [field2]

实例:

127.0.0.1:6379> hset user:100 id 100 name Hxstrive country CN
(integer) 3
127.0.0.1:6379> hmget user:100 id name
1) "100"
2) "Hxstrive"

HMSET命令

同时将多个 field-value (域-值)对设置到哈希表 key 中。语法如下:

HMSET key field1 value1 [field2 value2 ]

实例:

127.0.0.1:6379> hset user:100 id 100 name Hxstrive country CN
(integer) 0
127.0.0.1:6379> hmset user:100 id 200 name Helen
OK

HSET命令

将哈希表 key 中的字段 field 的值设为 value。语法如下:

HSET key field value

实例:

127.0.0.1:6379> hset user:100 id 100 name Hxstrive country CN
(integer) 3

HSETNX命令

只有在字段 field 不存在时,设置哈希表字段的值。语法如下:

HSETNX key field value

实例:

# name 字段不存在,设置成功
127.0.0.1:6379> hsetnx user:200 name Helen
(integer) 1
# name 字段已经存在,设置失败
127.0.0.1:6379> hsetnx user:200 name Helen
(integer) 0

HVALS命令

获取哈希表中所有值。语法如下:

HVALS key

实例:

127.0.0.1:6379> hgetall user:100
1) "id"
2) "100"
3) "name"
4) "Helen"
5) "country"
6) "CN"
127.0.0.1:6379> hvals user:100
1) "100"
2) "Helen"
3) "CN"

HSCAN命令

迭代哈希表中的键值对。语法如下:

HSCAN key cursor [MATCH pattern] [COUNT count]

注意:count 默认为10。

实例:

127.0.0.1:6379> hmset sites google "google.com" baidu "baidu.com" bing "bing.com"
OK
127.0.0.1:6379> hscan sites 0 match "*g*"
1) "0"
2) 1) "google"
  2) "google.com"
  3) "bing"
  4) "bing.com"
127.0.0.1:6379>

更多命令请访问 https://redis.io/commands  进行参考。

说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号