HMSET:一次为多个字段设置值
用户可以使用 HMSET 命令一次为散列中的多个字段设置值:
HMSET hash field value [field value ...]
HMSET 命令在设置成功时返回 OK。
比如,为了构建图 3-17 所示的散列,我们可能会执行以下 4 个 HSET 命令:
redis> HSET article::10086 title "greeting"
(integer) 1
redis> HSET article::10086 content "hello world"
(integer) 1
redis> HSET article::10086 author "peter"
(integer) 1
redis> HSET article::10086 created_at "1442744762.631885"
(integer) 1

Figure 1. 图3-17 存储文章数据的散列
但是接下来的这一条 HMSET 命令可以更方便地完成相同的工作:
redis> HMSET article::10086 title "greeting" content "hello world" author "peter" created_at "14
42744762.631885"
OK
此外,因为客户端在执行这条 HMSET 命令时只需要与 Redis 服务器进行一次通信,而上面的 4 条 HSET 命令则需要客户端与 Redis 服务器进行 4 次通信,所以前者的执行速度要比后者快得多。
使用新值覆盖旧值
如果用户给定的字段已经存在于散列当中,那么 HMSET 命令将使用用户给定的新值去覆盖字段已有的旧值。
比如对于 title 和 content 这两个已经存在于 article::10086 散列的字段来说:
redis> HGET article::10086 title
"greeting"
redis> HGET article::10086 content
"hello world"
如果我们执行以下命令:
redis> HMSET article::10086 title "Redis Tutorial" content "Redis is a data structure store, ..."
O
那么 title 字段和 content 字段已有的旧值将被新值覆盖:
redis> HGET article::10086 title
"Redis Tutorial"
redis> HGET article::10086 content
"Redis is a data structure store, ..."