STRLEN:获取字符串值的字节长度

通过对字符串键执行 STRLEN 命令,用户可以取得字符串键存储的值的字节长度:

STRLEN key

以下代码展示了如何使用 STRLEN 去获取不同字符串值的字节长度:

redis> GET number
"10086"
redis> STRLEN number -- number键的值长5字节
(integer) 5
redis> GET message
"hello world"
redis> STRLEN message -- message键的值长11字节
(integer) 11
redis> GET book
"The Design and Implementation of Redis"
redis> STRLEN book -- book键的值长38字节
(integer) 38

对于不存在的键,STRLEN 命令将返回 0:

redis> STRLEN not-exists-key
(integer) 0

其他信息

  • 复杂度:O(1)。

  • 版本要求:STRLEN 命令从 Redis 2.2.0 开始可用。