本文内容为 http://redis.io/commands/zincrby 的翻译、注解、例子扩充及其它修改。感谢 Redis 作者 Antirez 为开源社区作出的贡献,本文保证最新、最准、最全以表示对其的敬意。欢迎留言纠错、提示更新或支持。

ZINCRBY key increment member

将存储在 key 中的有序集合(Sorted set)中的某个成员(Member)member 加上增量 increment

Increments the score of member in the sorted set stored at key by increment.

如果成员 member 不存在于该有序集合,它将会被添加,且将增量 increment 作为它的分值(Score)(就好像之前的分值为 0.0)。

If member does not exist in the sorted set, it is added with increment as its score (as if its previous score was 0.0).

如果键 key 不存在,一个以指定的成员 member 作为其唯一成员的新的有序集合将被创建。

If key does not exist, a new sorted set with the specified member as its sole member is created.

如果键 key 存在,但所存储的不是有序集合,将会返回一个错误。

An error is returned when key exists but does not hold a sorted set.

分值 score 必须为一个数值(Numeric value)的字符串形式表示(String representation),且支持双精度浮点数(Double precision floating point numbers),也可以提供一个负值以表示减法操作。

The score value should be the string representation of a numeric value, and accepts double precision floating point numbers. It is possible to provide a negative value to decrement the score.
  • 返回成员 member 的新值,该值为以字符串形式表示的双精度浮点数。

  • redis> ZADD myzset 1 "one"
    (integer) 1
    redis> ZADD myzset 2 "two"
    (integer) 1
    redis> ZINCRBY myzset 2 "one"
    "3"
    redis> ZRANGE myzset 0 -1 WITHSCORES
    1) "two"
    2) "2"
    3) "one"
    4) "3"
  • 版本支持

    1.2.0+

    时间复杂度(Time complexity)

    O(log(N)),N 为有序集合中元素的个数。

    O(log(N)) where N is the number of elements in the sorted set.