Redis之zset型別及操作

stonebox1122發表於2017-08-11
zset是string型別的有序集合

zadd:向名稱為key的zset中新增元素member,score用於排序。如果該元素存在,則更新其順序。
127.0.0.1:6379> zadd myzset 1 one
(integer) 1
127.0.0.1:6379> zadd myzset 2 two
(integer) 1
127.0.0.1:6379> zadd myzset 3 two
(integer) 0
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "3"

zrem:刪除名稱為key的zset中的元素member。
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "3"
127.0.0.1:6379> zrem myzset two
(integer) 1
127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "1"

zincrby:如果在名稱為key的zset中已經存在元素member,則該元素的score增加increment,否則向該集合中新增該元素,其score的值為increment。
127.0.0.1:6379> zadd myzset2 1 one
(integer) 1
127.0.0.1:6379> zadd myzset2 2 two
(integer) 1
127.0.0.1:6379> zrange myzset2 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
127.0.0.1:6379> zincrby myzset2 2 one
"3"
127.0.0.1:6379> zrange myzset2 0 -1 withscores
1) "two"
2) "2"
3) "one"
4) "3"

zrank:返回名稱為key的zset中member元素的排名(按score從小到大排序)即下標。
127.0.0.1:6379> zadd myzset3 1 one
(integer) 1
127.0.0.1:6379> zadd myzset3 2 two
(integer) 1
127.0.0.1:6379> zadd myzset3 3 three
(integer) 1
127.0.0.1:6379> zadd myzset3 5 five
(integer) 1
127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "five"
8) "5"
127.0.0.1:6379> zrank myzset3 two
(integer) 1
127.0.0.1:6379> zrank myzset3 three
(integer) 2

zrevrank:返回名稱為key的zset中member元素的排名(按score從大到小排序)即下標
127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "five"
8) "5"
127.0.0.1:6379> zrevrank myzset3 five
(integer) 0

zrevrange:
127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "five"
8) "5"
127.0.0.1:6379> zrevrange myzset3 0 -1 withscores
1) "five"
2) "5"
3) "three"
4) "3"
5) "two"
6) "2"
7) "one"
8) "1"


zrangebyscore:
127.0.0.1:6379> zrangebyscore myzset3 2 3 withscores
1) "two"
2) "2"
3) "three"
4) "3"

zcount:
127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "five"
8) "5"
127.0.0.1:6379> zcount myzset3 2 3
(integer) 2

zcard:
127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "five"
8) "5"
127.0.0.1:6379> zcard myzset3
(integer) 4

zremrangebyrank:刪除集合中排名在給定區間的元素。
127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "five"
8) "5"
127.0.0.1:6379> zremrangebyrank myzset3 1 1
(integer) 1
127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "three"
4) "3"
5) "five"
6) "5"


zremrangbyscore:
127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "three"
4) "3"
5) "five"
6) "5"
127.0.0.1:6379> zremrangebyscore myzset3 4 5
(integer) 1
127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "three"
4) "3"

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28536251/viewspace-2143380/,如需轉載,請註明出處,否則將追究法律責任。

相關文章