HSTRLEN:获取字段值的字节长度

用户可以使用 HSTRLEN 命令获取给定字段值的字节长度:

HSTRLEN hash field

比如对于图 3-12 所示的 article::10086 散列来说,我们可以通过执行以下 HSTRLEN 命令取得 title、content、author 等字段值的字节长度:

redis> HSTRLEN article::10086 title
(integer) 8 -- title字段的值"greeting"长8个字节
redis> HSTRLEN article::10086 content
(integer) 11 -- content字段的值"hello world"长11个字节
redis> HSTRLEN article::10086 author
(integer) 5 -- author字段的值"peter"长6个字节
image 2025 01 02 17 58 37 615
Figure 1. 图3-12 使用散列存储文章数据

如果给定的字段或散列并不存在,那么 HSTRLEN 命令将返回 0 作为结果:

redis> HSTRLEN article::10086 last_updated_at -- 字段不存在
(integer) 0
redis> HSTRLEN not-exists-hash not-exists-key -- 散列不存在
(integer) 0

其他信息

  • 复杂度:O(1)。

  • 版本要求:HSTRLEN 命令从 Redis 3.2.0 版本开始可用。