语法

1
STRLEN key

可用版本

≥ 2.2.0

时间复杂度

$O(1)$

ACL类别

@read@string@fast

返回存储在 key 处的字符串值的长度。当 key 存储的值不是字符串类型时,则返回错误。

返回值

返回一个整数:

  • key 对应值的字符串长度;
  • 如果 key 不存在,则返回 0。

示例 1

返回键 user 存储的字符串的长度:

1
2
3
4
redis> SET user "JOHNSON LIN"
OK
redis> STRLEN user
(integer) 11

示例 2

键 nonexisting 不存在,STRLEN 操作返回 0:

1
2
3
4
redis> EXISTS nonexisting
(integer) 0
redis> STRLEN nonexisting
(integer) 0

示例 3

键 listkey 存储的值不是字符串类型,STRLEN 操作返回错误:

1
2
3
4
5
6
redis> RPUSH listkey "JOHNSON LIN"
(integer) 1
redis> TYPE listkey
list
redis> STRLEN listkey
(error) WRONGTYPE Operation against a key holding the wrong kind of value

(END)