add

英[æd] 美[æd]

vt.Add; add; add explanation; include…

vi. increase; do addition; accumulate; expand : added past participle: added

redis ZADD command syntax

Function:Add one or more member elements and their score values ​​to the ordered set key.

Syntax: ZADD key score member [[score member] [score member] ...]

Description: If a member has is a member of the ordered set, then update the score value of this member and re-insert the member element to ensure that the member is in the correct position. The score value can be an integer value or a double precision floating point number. If key does not exist, create an empty sorted set and perform ZADD operation. When key exists but is not an ordered set type, an error is returned. Before Redis 2.4, ZADD could only add one element at a time.

Available versions: >= 1.2.0

Time complexity: O(M*log(N)), N is The cardinality of the sequence set, M is the number of new members successfully added.

Return: The number of new members successfully added, excluding those updated and existing members.

redis ZADD command example

# 添加单个元素
redis> ZADD page_rank 10 google.com
(integer) 1
# 添加多个元素
redis> ZADD page_rank 9 baidu.com 8 bing.com
(integer) 2
redis> ZRANGE page_rank 0 -1 WITHSCORES
1) "bing.com"
2) "8"
3) "baidu.com"
4) "9"
5) "google.com"
6) "10"
# 添加已存在元素,且 score 值不变
redis> ZADD page_rank 10 google.com
(integer) 0
redis> ZRANGE page_rank 0 -1 WITHSCORES  # 没有改变
1) "bing.com"
2) "8"
3) "baidu.com"
4) "9"
5) "google.com"
6) "10"
# 添加已存在元素,但是改变 score 值
redis> ZADD page_rank 6 bing.com
(integer) 0
redis> ZRANGE page_rank 0 -1 WITHSCORES  # bing.com 元素的 score 值被改变
1) "bing.com"
2) "6"
3) "baidu.com"
4) "9"
5) "google.com"
6) "10"