Redis zincrby 命令用于将存储在 key 的排序集中的成员的分数增加或者减少一个值。如果排序集中不存在成员,则添加指定的增量值。
当key不存在,则创建一个以指定成员为唯一成员的新排序集。
当key存在但不包含排序集时,将返回错误。
语法:
zincrby key increment member
可用版本
>=1.2.0.
返回值
字符串,成员新的分数值(双精度浮点数),表示为字符串。
返回值类型
字符串
示例1:Redis zincrby:分数增加、减少
127.0.0.1:6379> zadd myvisit 1200 weibo.com 1800 baidu.com 1500 stackoverflow.com
(integer) 3
127.0.0.1:6379> zrevrange myvisit 0 -1 WITHSCORES
1) "baidu.com"
2) "1800"
3) "stackoverflow.com"
4) "1500"
5) "weibo.com"
6) "1200"
weibo.com 增加 500
127.0.0.1:6379> zincrby myvisit 500 weibo.com
"1700"
127.0.0.1:6379> zrevrange myvisit 0 -1 WITHSCORES
1) "baidu.com"
2) "1800"
3) "weibo.com"
4) "1700"
5) "stackoverflow.com"
6) "1500"
baidu.com 减少 200
127.0.0.1:6379> zincrby myvisit -200 baidu.com
"1600"
排序变了
127.0.0.1:6379> zrevrange myvisit 0 -1 WITHSCORES
1) "weibo.com"
2) "1700"
3) "baidu.com"
4) "1600"
5) "stackoverflow.com"
6) "1500"
示例2:Redis zincrby:使用小数
127.0.0.1:6379> zadd myliteracy 8.3 Canada 6.7 Brazil 5.1 India 4.2 Koria 7.9 Japan
(integer) 5
127.0.0.1:6379> zrevrange myliteracy 0 -1 WITHSCORES
1) "Canada"
2) "8.3000000000000007"
3) "Japan"
4) "7.9000000000000004"
5) "Brazil"
6) "6.7000000000000002"
7) "India"
8) "5.0999999999999996"
9) "Koria"
10) "4.2000000000000002"
127.0.0.1:6379> zincrby myliteracy 1.7 India
"6.7999999999999998"
示例3:Redis zincrby:
127.0.0.1:6379> zincrby dailyviewers 1 450205
"1"
127.0.0.1:6379> zincrby dailyviewers 1 450205
"2"
127.0.0.1:6379> zincrby dailyviewers 1 450205
"3"
127.0.0.1:6379> zincrby dailyviewers 1 450306
"1"
127.0.0.1:6379> zincrby dailyviewers 1 450306
"2"
127.0.0.1:6379> zrevrange dailyviewers 0 -1 WITHSCORES
1) "450205"
2) "3"
3) "450306"
4) "2"