轻工具箱
← 返回手册

HINCRBY

Redis手册 / 哈希表

HINCRBY

HINCRBY key field increment

为哈希表 key 中的域 field 的值加上增量 increment 。

增量也可以为负数,相当于对给定域进行减法操作。

如果 key 不存在,一个新的哈希表被创建并执行 HINCRBY 命令。

如果域 field 不存在,那么在执行命令前,域的值被初始化为 0 。

对一个储存字符串值的域 field 执行 HINCRBY 命令将造成一个错误。

本操作的值被限制在 64 位(bit)有符号数字表示之内。

可用版本:>= 2.0.0

时间复杂度: O(1)

返回值:

执行 HINCRBY 命令之后,哈希表 key 中域 field 的值。 1# increment 为正数2redis> HEXISTS counter page_view # 对空域进行设置3(integer) 04 5redis> HINCRBY counter page_view 2006(integer) 2007 8redis> HGET counter page_view9"200"10 11 12# increment 为负数13 14redis> HGET counter page_view15"200"16 17redis> HINCRBY counter page_view -5018(integer) 15019 20redis> HGET counter page_view21"150"22 23 24# 尝试对字符串值的域执行HINCRBY命令25 26redis> HSET myhash string hello,world # 设定一个字符串值27(integer) 128 29redis> HGET myhash string30"hello,world"31 32redis> HINCRBY myhash string 1 # 命令执行失败,错误。33(error) ERR hash value is not an integer34 35redis> HGET myhash string # 原值不变36"hello,world"```