Redis命令-有序集合-zadd

原文

http://redis.io/commands/zadd

简介

Add one or more members to a sorted set, or update its score if it already exists.

添加一个或多个成员到有序集合,如果成员已经存在则更新它的分数。

语法

ZADD key [NX|XX] [CH] [INCR] score member [score member ...]

版本

Available since 1.2.0.

自1.2.0版本可用。

时间复杂度

Time complexity: O(log(N)) for each item added, where N is the number of elements in the sorted set.

 O(log(N)):N是有序集合中成员的数量。

描述

Adds all the specified members with the specified scores to the sorted set stored at key. It is possible to specify multiple score / member pairs. If a specified member is already a member of the sorted set, the score is updated and the element reinserted at the right position to ensure the correct ordering.

添加所有带指定分数的指定成员到有序集合。可以添加多个分数/成员对。如果一个指定的成员已经是有序集合的成员,那么它的分数会被更新并且这个成员会被重新插入到正确的位置来保证准确的排序。

 If key does not exist, a new sorted set with the specified members as sole members is created, like if the sorted set was empty. If the key exists but does not hold a sorted set, an error is returned.

如果key不存在,带指定成员的新有序集合被创建,类似这个有序集合是空的。如果key已经存在但不是有序集合,返回错误。

 The score values should be the string representation of a double precision floating point number. +inf and -inf values are valid values as well.

分数是双精度浮点数的字符串表示形式。+inf和-inf也是有效值。

ZADD选项

Redis 3.0.2及以上版本支持。

ZADD supports a list of options, specified after the name of the key and before the first score argument. Options are:

  • XX: Only update elements that already exist. Never add elements.
  • NX: Don't update already existing elements. Always add new elements.
  • CH: Modify the return value from the number of new elements added, to the total number of elements changed (CH is an abbreviation of changed). Changed elements are new elements added and elements already existing for which the score was updated. So elements specified in the command line having the same score as they had in the past are not counted. Note: normally the return value of ZADD only counts the number of new elements added.
  • INCR: When this option is specified ZADD acts like ZINCRBY. Only one score-element pair can be specified in this mode.

 ZADD支持一系列选项,在key名称之后和第一个分数参数之前进行指定。选项包括:

  • XX:仅更新已经存在的元素。从不添加元素。
  • NX:不更新已经存在的元素。总是添加新元素。
  • CH:修改返回值,从添加的新元素数量到变更元素的数量(CH是changed的缩写)。变更元素包括添加的新元素、以及已经存在且分数被更新的元素。因此,如果指定的元素分数和该元素的原有分数相等,那么这个元素不计算在内。注意:正常情况下,ZADD的返回值仅计算添加的新元素的数量。
  • INCR:当指定这个选项时,ZADD就像ZINCRBY。这个模式下,仅可以指定一个分数/成员对。

 返回值

 Integer reply, specifically:

  • The number of elements added to the sorted sets, not including elements already existing for which the score was updated.

If the INCR option is specified, the return value will be Bulk string reply:

  • the new score of member (a double precision floating point number), represented as string.

整数:添加到有序集合的元素数量,不包括已经存在且分数被更新的元素。
如果指定INCR选项,返回值是字符串:成员的新分数(双精度浮点数),表示为字符串。

 历史

 >= 2.4: Accepts multiple elements. In Redis versions older than 2.4 it was possible to add or update a single member per call.

2.4及以上版本,这个命令可以接受多个元素。2.4以前版本每次只可以添加或更新一个成员。

例子

redis>  ZADD myzset 1 "one"
(integer) 1
redis>  ZADD myzset 1 "uno"
(integer) 1
redis>  ZADD myzset 2 "two" 3 "three"
(integer) 2
redis>  ZRANGE myzset 0 -1 WITHSCORES
1) "one"
2) "1"
3) "uno"
4) "1"
5) "two"
6) "2"
7) "three"
8) "3"
redis> 

猜你喜欢

转载自gitzhangyl.iteye.com/blog/2288585