Redis Incrby 命令


Redis Incrby 命令将 key 中储存的数字加上指定的增量值。

如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCRBY 命令。

如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。

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

语法

redis Incrby 命令基本语法如下:

  1. redis 127.0.0.1:6379> INCRBY KEY_NAME INCR_AMOUNT

可用版本

>= 1.0.0

返回值

加上指定的增量值之后, key 的值。

实例

  1. # key 存在且是数字值
  2.  
  3. redis> SET rank 50
  4. OK
  5.  
  6. redis> INCRBY rank 20
  7. (integer) 70
  8.  
  9. redis> GET rank
  10. "70"
  11.  
  12.  
  13. # key 不存在时
  14.  
  15. redis> EXISTS counter
  16. (integer) 0
  17.  
  18. redis> INCRBY counter 30
  19. (integer) 30
  20.  
  21. redis> GET counter
  22. "30"
  23.  
  24.  
  25. # key 不是数字值时
  26.  
  27. redis> SET book "long long ago..."
  28. OK
  29.  
  30. redis> INCRBY book 200
  31. (error) ERR value is not an integer or out of range