RedisSortedSet有序集合存储操作方法-创新互联
欢迎大家加入 459479177QQ群进行交流
从策划到设计制作,每一步都追求做到细腻,制作可持续发展的企业网站。为客户提供成都网站建设、网站建设、网站策划、网页设计、空间域名、网络空间、网络营销、VI设计、 网站改版、漏洞修补等服务。为客户提供更好的一站式互联网解决方案,以客户的口碑塑造优易品牌,携手广大客户,共同发展进步。这一章是Redis Sorted Set有序集合五种方式的最后一种,
首先我们使用redis-cli进入redis
[root@web1 ~]# redis-cli 127.0.0.1:6379>1>zadd
方法:zadd key source1 [key sourceN...]
描述:添加一个或多个有序集合成员元素,如果存在则更新成员的分数值,并插入此成语,确保该成员在正确的位置,如果key不存在则创建一个新的key,然后在执行zadd操作
127.0.0.1:6379> zadd zkey1 1 Sunshine \\第一次成员分数1 (integer) 1 127.0.0.1:6379> zrange zkey1 0 -1 withscores 1) "Sunshine" 2) "1" 127.0.0.1:6379> zadd zkey1 3 Sunshine \\成员分数3 (integer) 0 127.0.0.1:6379> zrange zkey1 0 -1 withscores \\成员分数变成3,成员元素没变 1) "Sunshine" 2) "3"2>zcard
方法:zcard key
描述:查看key的成员元素个数,一个分数值对应多个元素
127.0.0.1:6379> zadd zkey 1 Sunshine 1 Sunshine2 1 Sunshine3 (integer) 3 127.0.0.1:6379> zcount zkey 1 3 (integer) 3 127.0.0.1:6379> zrange zkey 0 -1 withscores 1) "Sunshine" 2) "1" 3) "Sunshine2" 4) "1" 5) "Sunshine3" 6) "1"3>zcount
方法:zacount key min max
描述:统计指定集合的成员元素
127.0.0.1:6379> zrange zkey2 0 -1 withscores (empty list or set) 127.0.0.1:6379> zadd zkey2 1 Sushine 1 Sunshine0 (integer) 2 127.0.0.1:6379> zrange zkey2 0 -1 withscores 1) "Sunshine0" 2) "1" 3) "Sushine" 4) "1" 127.0.0.1:6379> zcount zkey2 1 2 (integer) 24>zincrby
方法:zincrby key increment member
描述:对有序集合指定成员分数值增量,如果-则为减
127.0.0.1:6379> zrange zkey2 0 -1 withscores 1) "Sunshine0" 2) "1" 3) "Sushine" 4) "1" 5) "Sunshine" 6) "5" 127.0.0.1:6379> zincrby zkey2 5 "Sunshine" "10" 127.0.0.1:6379> zrange zkey2 0 -1 withscores 1) "Sunshine0" 2) "1" 3) "Sushine" 4) "1" 5) "Sunshine" 6) "10" 127.0.0.1:6379> zincrby zkey2 -5 "Sunshine" "5" 127.0.0.1:6379> zrange zkey2 0 -1 withscores 1) "Sunshine0" 2) "1" 3) "Sushine" 4) "1" 5) "Sunshine" 6) "5"5>zintersotre
方法:zintersotre destination numkeys key [key...] [weights weighe [weight...]] [aggregate sun|min|max]
描述:把后面destination后面的集合相同成员分数值相加并添加至destinatio集合成员
127.0.0.1:6379> zrange zkey3 0 -1 withscores 1) "Sunshine" 2) "11" 3) "SunshineBoy" 4) "44" 127.0.0.1:6379> zrange zkey4 0 -1 withscores 1) "Sunshine" 2) "22" 3) "SunshineBoy" 4) "33" 127.0.0.1:6379> zinterstore zkey5 2 zkey4 zkey3 (integer) 2 127.0.0.1:6379> zrange zkey5 0 -1 withscores 1) "Sunshine" 2) "33" 3) "SunshineBoy" 4) "77"6>zlexcount
方法:zlexcount key min max
描述:指定有序集合字典区间成员数
127.0.0.1:6379> zrange zkey6 0 -1 withscores 1) "a" 2) "0" 3) "b" 4) "0" 5) "c" 6) "0" 7) "d" 8) "0" 9) "e" 10) "0" 11) "f" 12) "0" 13) "g" 14) "0" 127.0.0.1:6379> zlexcount zkey6 [b [f (integer) 5 127.0.0.1:6379> zlexcount zkey6 [b [g (integer) 67>zrange
方法:zrange key start end [withscores]
描述:指定查看有序集合区间成员
127.0.0.1:6379> zrange zkey6 0 -1 withscores 1) "a" 2) "0" 3) "b" 4) "0" 5) "c" 6) "0" 7) "d" 8) "0" 9) "e" 10) "0" 11) "f" 12) "0" 13) "g" 14) "0" 127.0.0.1:6379> zrange zkey6 0 3 withscores 1) "a" 2) "0" 3) "b" 4) "0" 5) "c" 6) "0" 7) "d" 8) "0"8>zrangebylex
方法:zrangebylex key min max [limit offset count]
描述:通过字典区间返回有序集合成员
127.0.0.1:6379> zrangebylex zkey6 - [c 1) "a" 2) "b" 3) "c" 127.0.0.1:6379> zrangebylex zkey6 - + 1) "a" 2) "b" 3) "c" 4) "d" 5) "e" 6) "f" 7) "g" 127.0.0.1:6379> zrange zkey6 0 -1 withscores 1) "a" 2) "0" 3) "b" 4) "0" 5) "c" 6) "0" 7) "d" 8) "0" 9) "e" 10) "0" 11) "f" 12) "0" 13) "g" 14) "0"9>zrangesotre
方法:zrangesotre key min max [withscores] [limit offsec count]
描述:通过成员分数值返回区间成员
127.0.0.1:6379> zrangebyscore zkey10 -inf +inf \\显示整个有序集合成员 1) "jack" 2) "tom" 3) "Sunshine" 127.0.0.1:6379> zrangebyscore zkey10 -inf +inf withscores \\显示整个有序集合成员的score 1) "jack" 2) "2000" 3) "tom" 4) "8000" 5) "Sunshine" 6) "15000" 127.0.0.1:6379> zrangebyscore zkey10 -inf 5000 withscores \\显示小于等于5000分数值的成员 1) "jack" 2) "2000" 127.0.0.1:6379> zrangebyscore zkey10 (5000 20000 withscores \\显示分数值5000至20000之间的成员 1) "tom" 2) "8000" 3) "Sunshine" 4) "15000" 127.0.0.1:6379>10>zrank
方法:zrank key member
描述:显示有序集合成员的索引(index或者叫下标)
127.0.0.1:6379> zrank zkey10 tom (integer) 1 127.0.0.1:6379> zrank zkey10 Sunshine (integer) 2 127.0.0.1:6379> zrank zkey10 jack (integer) 0 127.0.0.1:6379> zrange zkey10 0 -1 withscores 1) "jack" 2) "2000" 3) "tom" 4) "8000" 5) "Sunshine" 6) "15000"11>zrem
方法:zrem key member [member...]
描述:移除一个有序集合成员或多个
127.0.0.1:6379> zrange zkey10 0 -1 withscores 1) "jack" 2) "2000" 3) "tom" 4) "8000" 5) "Sunshine" 6) "15000" 127.0.0.1:6379> zrem zkey10 jack (integer) 1 127.0.0.1:6379> zrem zkey10 tom Sunshine (integer) 2 127.0.0.1:6379> zrange zkey10 0 -1 withscores (empty list or set)12>zremrangebylex
方法:zremrangebylex key min max
描述:Zremrangebylex 命令用于移除有序集合中给定的字典区间的所有成员
127.0.0.1:6379> zrange zkey2 0 -1 withscores 1) "a" 2) "1" 3) "b" 4) "1" 5) "c" 6) "1" 7) "d" 8) "1" 9) "e" 10) "1" 11) "f" 12) "1" 13) "g" 14) "1" 15) "h" 16) "1" 17) "l" 18) "1" 19) "m" 20) "1" 127.0.0.1:6379> zremrangebylex zkey2 [a [c (integer) 3 127.0.0.1:6379> zrange zkey2 0 -1 withscores 1) "d" 2) "1" 3) "e" 4) "1" 5) "f" 6) "1" 7) "g" 8) "1" 9) "h" 10) "1" 11) "l" 12) "1" 13) "m" 14) "1" 127.0.0.1:6379> zremrangebylex zkey2 [d [h (integer) 5 127.0.0.1:6379> zrange zkey2 0 -1 withscores 1) "l" 2) "1" 3) "m" 4) "1"13>zremrangeburank
方法:zremrangebyrank key start end
描述:移除有序集合中给定的排名区间的所有成员(指定是指下标)
127.0.0.1:6379> zrange zkey11 0 -1 withscores 1) "Sunshine" 2) "100" 3) "SunshineBoy" 4) "200" 5) "SnshineBoySZF" 6) "300" 127.0.0.1:6379> zremrangebyrank zkey11 0 0 (integer) 1 127.0.0.1:6379> zrange zkey11 0 -1 withscores 1) "SunshineBoy" 2) "200" 3) "SnshineBoySZF" 4) "300" 127.0.0.1:6379> zremrangebyrank zkey11 0 0 (integer) 1 127.0.0.1:6379> zrange zkey11 0 -1 withscores 1) "SnshineBoySZF" 2) "300" 127.0.0.1:6379> zadd zkey11 500 hehe (integer) 1 127.0.0.1:6379> zrange zkey11 0 -1 withscores 1) "SnshineBoySZF" 2) "300" 3) "hehe" 4) "500" 127.0.0.1:6379> zremrangebyrank zkey11 0 1 (integer) 2 127.0.0.1:6379> zrange zkey11 0 -1 withscores (empty list or set)14>zremrangebyscore
方法:zremrangebyscore key min max
描述:指定有序集合,移除(score)区间的成员
127.0.0.1:6379> zadd zkey12 100 Sunshine 200 SunshineBoy 300 SnshineBoySZF (integer) 3 127.0.0.1:6379> zrange zkey12 0 -1 withscores 1) "Sunshine" 2) "100" 3) "SunshineBoy" 4) "200" 5) "SnshineBoySZF" 6) "300" 127.0.0.1:6379> zremrangebyscore zkey12 200 300 (integer) 2 127.0.0.1:6379> zrange zkey12 0 -1 withscores 1) "Sunshine" 2) "100"15>zrevrage
方法:zrevrange key start end [withscores]
描述:指定有序集合倒序排序
127.0.0.1:6379> zrange zkey12 0 -1 withscores 1) "Sunshine" 2) "100" 3) "SunshineBoy" 4) "200" 5) "SnshineBoySZF" 6) "300" 127.0.0.1:6379> zrevrange zkey12 0 -1 withscores 1) "SnshineBoySZF" 2) "300" 3) "SunshineBoy" 4) "200" 5) "Sunshine" 6) "100"16>zrevrangebybscore
方法:zrevrangebyscore key min max [withscores] [limit offsec count]
描述:指定有序集合,按指定分数值从高到底排序(通俗讲就是,按照指定值,倒序)
127.0.0.1:6379> zrevrange zkey12 0 -1 withscores
1) "SnshineBoySZF"
2) "300"
3) "SunshineBoy"
4) "200"
5) "Sunshine"
6) "100"
127.0.0.1:6379> zrevrangebyscore zkey12 +inf -inf \\倒序排列
1) "SnshineBoySZF"
2) "SunshineBoy"
3) "Sunshine"
127.0.0.1:6379> zrevrangebyscore zkey12 200 100
1) "SunshineBoy"
2) "Sunshine"
17>zrevrank
方法:zrevrank key start end [withscores] [limit offsec count]
127.0.0.1:6379> zrange zkey 0 -1 withscores 1) "sunshine" 2) "100" 3) "tom" 4) "200" 5) "jack" 6) "500" 7) "hehe" 8) "2000" 127.0.0.1:6379> zrevrank zkey tom (integer) 2 127.0.0.1:6379> zrevrank zkey hehe (integer) 018>zscore
方法:zscore key member
描述:返回有序集合中,成员的分数值
127.0.0.1:6379> zrange zkey 0 -1 withscores 1) "sunshine" 2) "100" 3) "tom" 4) "200" 5) "jack" 6) "500" 7) "hehe" 8) "2000" 127.0.0.1:6379> zscore zkey tom "200" 127.0.0.1:6379> zscore zkey jack "500"19>zunionstore
方法:ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] {直接复制官方,官方的例子没整明白,容后补写指理论}
描述:计算给定的一个或多个有序集的并集,并存储在新的 key 中。
127.0.0.1:6379> zadd zkey1 100 a 200 b (integer) 2 127.0.0.1:6379> zadd zkey2 300 c 400 b 500 e (integer) 1 127.0.0.1:6379> zunionstore zkey3 2 zkey1 zkey2 (integer) 4 127.0.0.1:6379> zrange zkey3 0 -1 withscores 1) "a" 2) "100" 3) "c" 4) "300" 5) "e" 6) "500" 7) "b" 8) "600"20>zscan
方法:ZSCAN key cursor [MATCH pattern] [COUNT count]
描述:迭代有序集合中的元素(包括元素成员和元素分值)
127.0.0.1:6379> zrange zkey 0 -1 withscores 1) "No_s\x94ql" 2) "1" 3) "abc" 4) "1" 5) "ao" 6) "1" 7) "apche" 8) "1" 9) "mysql" 10) "1" 11) "nginx" 12) "1" 127.0.0.1:6379> zscan zkey 0 match a* count 1 1) "0" 2) 1) "abc" 2) "1" 3) "ao" 4) "1" 5) "apche" 6) "1"至此Redis sorted set有序集合就介绍完毕了!
欢迎大家加入 459479177QQ群进行交流
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前名称:RedisSortedSet有序集合存储操作方法-创新互联
网页地址:http://scyanting.com/article/djhphp.html