十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本文今天主要是集合(Sorted Set)的方法的使用,以及redis对应的Java实现该怎么用。因为篇幅问题,我这里写了一个测试类,引入RedisTemplate对象,后面例子里就不一一引入了。大家理解就行,如果大家还不知道如何通过Spring Boot整合redis则可以查看我之前的文章:SpringBoot整合redis(redis支持单节点和集群),这一章注定是枯燥的,但是可能也是实际中反而用得多的,大家看着办了。
package com.alian.datastruct;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class RedisSortedSetTest {@Autowired
private RedisTemplateredisTemplate;
}
二、常用方法
2.1、ZADD语法
ZADD KEY_NAME SCORE1 MEMBER1... SCOREn MEMBERn
命令操作
127.0.0.1:6379>zadd zset1 95.0 taojianwen
(integer) 1
127.0.0.1:6379>zadd zset1 92.0 tangpeng 91.0 wanglin 90.0 luokaocong
(integer) 3
127.0.0.1:6379>zrange zset1 0 -1
1) "luokaocong"
2) "wanglin"
3) "tangpeng"
4) "taojianwen"
Java操作
@Test
public void zAdd() {String redisKey = "zSet1";
redisTemplate.delete(redisKey);
// 添加一个成员
redisTemplate.opsForZSet().add(redisKey, "taojianwen",95.0);
// 添加多个成员
Set>tuple=new HashSet<>();
tuple.add(new DefaultTypedTuple<>("tangpeng", 92.0));
tuple.add(new DefaultTypedTuple<>("wanglin", 91.0));
tuple.add(new DefaultTypedTuple<>("luokaocong", 90.0));
redisTemplate.opsForZSet().add(redisKey,tuple);
// 获取成员的信息
Set
获取集合的信息:[luokaocong, wanglin, tangpeng, taojianwen]
2.2、ZREM语法
ZREM KEY_NAME MEMBER [MEMBER ...]
命令操作
127.0.0.1:6379>zadd zset2 95.0 taojianwen 92.0 tangpeng 91.0 wanglin 90.0 luokaocong
(integer) 4
127.0.0.1:6379>zrem zset2 luokaocong
(integer) 1
127.0.0.1:6379>zrange zset2 0 -1
1) "wanglin"
2) "tangpeng"
3) "taojianwen"
Java操作
@Test
public void zRem() {String redisKey = "zSet2";
redisTemplate.delete(redisKey);
// 添加多个成员
Set>tuple=new HashSet<>();
tuple.add(new DefaultTypedTuple<>("taojianwen", 95.0));
tuple.add(new DefaultTypedTuple<>("tangpeng", 92.0));
tuple.add(new DefaultTypedTuple<>("wanglin", 91.0));
tuple.add(new DefaultTypedTuple<>("luokaocong", 90.0));
redisTemplate.opsForZSet().add(redisKey,tuple);
// 移除成员
Long num = redisTemplate.opsForZSet().remove(redisKey, "luokaocong");
log.info("移除成员的个数:{}", num);
// 获取成员的信息
Set
移除成员的个数:1
获取集合的信息:[wanglin, tangpeng, taojianwen]
2.3、ZSCORE语法
ZSCORE KEY_NAME MEMBER
命令操作
127.0.0.1:6379>zadd zset3 95.0 taojianwen 92.0 tangpeng 91.0 wanglin 90.0 luokaocong
(integer) 4
127.0.0.1:6379>zscore zset3 taojianwen
"95"
Java操作
@Test
public void zScore() {String redisKey = "zSet3";
redisTemplate.delete(redisKey);
// 添加多个成员
Set>tuple=new HashSet<>();
tuple.add(new DefaultTypedTuple<>("taojianwen", 95.0));
tuple.add(new DefaultTypedTuple<>("tangpeng", 92.0));
tuple.add(new DefaultTypedTuple<>("wanglin", 91.0));
tuple.add(new DefaultTypedTuple<>("luokaocong", 90.0));
redisTemplate.opsForZSet().add(redisKey,tuple);
// 获取成员的分数
Double score = redisTemplate.opsForZSet().score(redisKey, "taojianwen");
log.info("获取成员的分数:{}", score);
}
获取成员的分数:95.0
2.4、ZINCRBY语法
ZINCRBY KEY_NAME increment MEMBER
命令操作
127.0.0.1:6379>zadd zset4 95.0 taojianwen 92.0 tangpeng 91.0 wanglin 90.0 luokaocong
(integer) 4
127.0.0.1:6379>zincrby zset4 2 taojianwen
"97"
127.0.0.1:6379>zincrby zset4 2 tangpeng
"94"
Java操作
@Test
public void zIncrBy() {String redisKey = "zSet4";
redisTemplate.delete(redisKey);
// 添加多个成员
Set>tuple=new HashSet<>();
tuple.add(new DefaultTypedTuple<>("taojianwen", 95.0));
tuple.add(new DefaultTypedTuple<>("tangpeng", 92.0));
tuple.add(new DefaultTypedTuple<>("wanglin", 91.0));
tuple.add(new DefaultTypedTuple<>("luokaocong", 90.0));
redisTemplate.opsForZSet().add(redisKey,tuple);
// 获取成员的分数
redisTemplate.opsForZSet().incrementScore(redisKey, "taojianwen",2);
redisTemplate.opsForZSet().incrementScore(redisKey, "tangpeng",2);
// 获取成员的分数
Double score = redisTemplate.opsForZSet().score(redisKey, "taojianwen");
log.info("成员taojianwen的分数:{}", score);
Double score2 = redisTemplate.opsForZSet().score(redisKey, "tangpeng");
log.info("成员tangpeng的分数:{}", score2);
}
成员taojianwen的分数:97.0
成员tangpeng的分数:94.0
2.5、ZCARD语法
ZCARD KEY_NAME
命令操作
127.0.0.1:6379>zadd zset5 95.0 taojianwen 92.0 tangpeng 91.0 wanglin 90.0 luokaocong
(integer) 4
127.0.0.1:6379>zcard zset5
(integer) 4
Java操作
@Test
public void zCard() {String redisKey = "zSet5";
redisTemplate.delete(redisKey);
// 添加多个成员
Set>tuple=new HashSet<>();
tuple.add(new DefaultTypedTuple<>("taojianwen", 95.0));
tuple.add(new DefaultTypedTuple<>("tangpeng", 92.0));
tuple.add(new DefaultTypedTuple<>("wanglin", 91.0));
tuple.add(new DefaultTypedTuple<>("luokaocong", 90.0));
redisTemplate.opsForZSet().add(redisKey,tuple);
// 获取成员的分数
Long size = redisTemplate.opsForZSet().size(redisKey);
log.info("成员的个数:{}", size);
}
成员的个数:4
2.6、ZRANK、ZREVRANK语法
ZRANK KEY_NAME MEMBER
ZREVRANK KEY_NAME MEMBER
命令操作
127.0.0.1:6379>zadd zset6 95.0 taojianwen 92.0 tangpeng 91.0 wanglin 90.0 luokaocong
(integer) 4
127.0.0.1:6379>zrank zset6 taojianwen
(integer) 3
127.0.0.1:6379>zrank zset6 luokaocong
(integer) 0
127.0.0.1:6379>zrevrank zset6 taojianwen
(integer) 0
127.0.0.1:6379>zrevrank zset6 luokaocong
(integer) 3
ZRANK
排名 | 0 | 1 | 2 | 3 |
---|---|---|---|---|
分值 | 90.0 | 91.0 | 92.0 | 95.0 |
成员 | luokaocong | wanglin | tangpeng | taojianwen |
ZREVRANK
排名 | 0 | 1 | 2 | 3 |
---|---|---|---|---|
分值 | 95.0 | 92.0 | 91.0 | 90.0 |
成员 | taojianwen | tangpeng | wanglin | luokaocong |
Java操作
@Test
public void zRankAndZRevRank() {String redisKey = "zSet6";
redisTemplate.delete(redisKey);
// 添加多个成员
Set>tuple=new HashSet<>();
tuple.add(new DefaultTypedTuple<>("taojianwen", 95.0));
tuple.add(new DefaultTypedTuple<>("tangpeng", 92.0));
tuple.add(new DefaultTypedTuple<>("wanglin", 91.0));
tuple.add(new DefaultTypedTuple<>("luokaocong", 90.0));
redisTemplate.opsForZSet().add(redisKey,tuple);
// 返回有序集中指定成员的排名
Long rank1 = redisTemplate.opsForZSet().rank(redisKey,"taojianwen");
log.info("有序集中指定成员的(升序)排名为:{}", rank1);
Long rank2 = redisTemplate.opsForZSet().rank(redisKey,"luokaocong");
log.info("有序集中指定成员的(升序)排名为:{}", rank2);
Long rank3 = redisTemplate.opsForZSet().reverseRank(redisKey, "taojianwen");
log.info("有序集中指定成员的(降序)排名为:{}", rank3);
Long rank4 = redisTemplate.opsForZSet().reverseRank(redisKey, "luokaocong");
log.info("有序集中指定成员的(降序)排名为:{}", rank4);
}
有序集中指定成员的(升序)排名为:3
有序集中指定成员的(升序)排名为:0
有序集中指定成员的(降序)排名为:0
有序集中指定成员的(降序)排名为:3
2.7、ZRANGE、ZREVRANGE语法
ZRANGE KEY_NAME start stop [WITHSCORES]
ZREVRANGE KEY_NAME start stop [WITHSCORES]
命令操作
127.0.0.1:6379>zadd zset7 95.0 taojianwen 92.0 tangpeng 91.0 wanglin 90.0 luokaocong
(integer) 4
127.0.0.1:6379>zrange zset7 0 -1
1) "luokaocong"
2) "wanglin"
3) "tangpeng"
4) "taojianwen"
127.0.0.1:6379>zrevrange zset7 0 -1
1) "taojianwen"
2) "tangpeng"
3) "wanglin"
4) "luokaocong"
ZRANGE
索引 | 0 | 1 | 2 | 3 |
---|---|---|---|---|
分值 | 90.0 | 91.0 | 92.0 | 95.0 |
成员 | luokaocong | wanglin | tangpeng | taojianwen |
ZREVRANGE
索引 | 0 | 1 | 2 | 3 |
---|---|---|---|---|
分值 | 95.0 | 92.0 | 91.0 | 90.0 |
成员 | taojianwen | tangpeng | wanglin | luokaocong |
从 到 -1 就是全部
Java操作
@Test
public void zRangeAndZRevRange() {String redisKey = "zSet7";
redisTemplate.delete(redisKey);
// 添加多个成员
Set>tuple=new HashSet<>();
tuple.add(new DefaultTypedTuple<>("taojianwen", 95.0));
tuple.add(new DefaultTypedTuple<>("tangpeng", 92.0));
tuple.add(new DefaultTypedTuple<>("wanglin", 91.0));
tuple.add(new DefaultTypedTuple<>("luokaocong", 90.0));
redisTemplate.opsForZSet().add(redisKey,tuple);
// 返回有序集中指定区间的成员
Set
有序集中指定区间的成员(分值升序):[luokaocong, wanglin, tangpeng, taojianwen]
有序集中指定区间的成员(分值降序):[taojianwen, tangpeng, wanglin, luokaocong]
2.8、ZRANGEBYSCORE、ZREVRANGEBYSCORE语法
ZRANGEBYSCORE KEY_NAME min max [WITHSCORES] [LIMIT offset count]
ZREVRANGEBYSCORE KEY_NAME max min [WITHSCORES] [LIMIT offset count]
命令操作
127.0.0.1:6379>zadd zset8 95.0 taojianwen 92.0 tangpeng 91.0 wanglin 90.0 luokaocong
(integer) 4
127.0.0.1:6379>zrangebyscore zset8 91 92 WITHSCORES
1) "wanglin"
2) "91"
3) "tangpeng"
4) "92"
127.0.0.1:6379>zrevrangebyscore zset8 92 91 WITHSCORES
1) "tangpeng"
2) "92"
3) "wanglin"
4) "91"
#查全部
127.0.0.1:6379>zrangebyscore zset8 -INF +INF WITHSCORES
1) "luokaocong"
2) "90"
3) "wanglin"
4) "91"
5) "tangpeng"
6) "92"
7) "taojianwen"
8) "95"
#查大于91小于等于95的
127.0.0.1:6379>zrangebyscore zset8 (91 95 WITHSCORES
1) "tangpeng"
2) "92"
3) "taojianwen"
4) "95"
Java操作
@Test
public void zRangeBySoreAndZRevRangeBySore() {String redisKey = "zSet8";
redisTemplate.delete(redisKey);
// 添加多个成员
Set>tuple=new HashSet<>();
tuple.add(new DefaultTypedTuple<>("taojianwen", 95.0));
tuple.add(new DefaultTypedTuple<>("tangpeng", 92.0));
tuple.add(new DefaultTypedTuple<>("wanglin", 91.0));
tuple.add(new DefaultTypedTuple<>("luokaocong", 90.0));
redisTemplate.opsForZSet().add(redisKey,tuple);
// 通过分数返回有序集合指定区间内的成员
Setrange = redisTemplate.opsForZSet().rangeByScore(redisKey, 91.0, 92.0);
log.info("通过分数返回有序集合指定区间内的成员(分值升序):{}", range);
Setrange2 = redisTemplate.opsForZSet().reverseRangeByScore(redisKey, 91.0, 92.0);
log.info("通过分数返回有序集合指定区间内的成员(分值降序):{}", range2);
}
通过分数返回有序集合指定区间内的成员(分值升序):[wanglin, tangpeng]
通过分数返回有序集合指定区间内的成员(分值降序):[tangpeng, wanglin]
2.9、ZCOUNT语法
ZCOUNT KEY_NAME min max
命令操作
127.0.0.1:6379>zadd zset9 95.0 taojianwen 92.0 tangpeng 91.0 wanglin 90.0 luokaocong
(integer) 4
127.0.0.1:6379>zcount zset9 91 92
(integer) 2
127.0.0.1:6379>zcount zset9 90 95
(integer) 4
Java操作
@Test
public void zCount() {String redisKey = "zSet9";
redisTemplate.delete(redisKey);
// 添加多个成员
Set>tuple=new HashSet<>();
tuple.add(new DefaultTypedTuple<>("taojianwen", 95.0));
tuple.add(new DefaultTypedTuple<>("tangpeng", 92.0));
tuple.add(new DefaultTypedTuple<>("wanglin", 91.0));
tuple.add(new DefaultTypedTuple<>("luokaocong", 90.0));
redisTemplate.opsForZSet().add(redisKey,tuple);
// 返回指定区间分数的成员数
Long count = redisTemplate.opsForZSet().count(redisKey, 91.0, 92.0);
log.info("返回指定区间分数的成员数:{}", count);
Long count2 = redisTemplate.opsForZSet().count(redisKey, 90.0, 95.0);
log.info("返回指定区间分数的成员数:{}", count2);
}
返回指定区间分数的成员数:2
返回指定区间分数的成员数:4
2.10、ZREMRANGEBYRANK语法
ZREMRANGEBYRANK KEY_NAME start stop
命令操作
127.0.0.1:6379>zadd zset10 95.0 taojianwen 92.0 tangpeng 91.0 wanglin 90.0 luokaocong
(integer) 4
127.0.0.1:6379>zremrangebyrank zset10 1 2
(integer) 2
127.0.0.1:6379>zrange zset10 0 -1 withscores
1) "luokaocong"
2) "90"
3) "taojianwen"
4) "95"
Java操作
@Test
public void zRemRangeByRank() {String redisKey = "zSet10";
redisTemplate.delete(redisKey);
// 添加多个成员
Set>tuple=new HashSet<>();
tuple.add(new DefaultTypedTuple<>("taojianwen", 95.0));
tuple.add(new DefaultTypedTuple<>("tangpeng", 92.0));
tuple.add(new DefaultTypedTuple<>("wanglin", 91.0));
tuple.add(new DefaultTypedTuple<>("luokaocong", 90.0));
redisTemplate.opsForZSet().add(redisKey,tuple);
// 移除有序集合中给定的排名区间的所有成员
Long num = redisTemplate.opsForZSet().removeRange(redisKey, 1, 2);
log.info("移除的成员数:{}", num);
// 获取成员的信息
Setrange = redisTemplate.opsForZSet().range(redisKey, 0, -1);
log.info("获取集合的信息:{}", range);
}
移除的成员数:2
获取集合的信息:[luokaocong, taojianwen]
2.11、ZREMRANGEBYSCORE语法
ZREMRANGEBYSCORE KEY_NAME min max
命令操作
127.0.0.1:6379>zadd zset11 95.0 taojianwen 92.0 tangpeng 91.0 wanglin 90.0 luokaocong
(integer) 4
127.0.0.1:6379>zremrangebyscore zset11 91 96
(integer) 3
127.0.0.1:6379>zrange zset11 0 -1 withscores
1) "luokaocong"
2) "90"
Java操作
@Test
public void zRemRangeByScore() {String redisKey = "zSet11";
redisTemplate.delete(redisKey);
// 添加多个成员
Set>tuple=new HashSet<>();
tuple.add(new DefaultTypedTuple<>("taojianwen", 95.0));
tuple.add(new DefaultTypedTuple<>("tangpeng", 92.0));
tuple.add(new DefaultTypedTuple<>("wanglin", 91.0));
tuple.add(new DefaultTypedTuple<>("luokaocong", 90.0));
redisTemplate.opsForZSet().add(redisKey,tuple);
// 移除有序集合中给定的分数区间的所有成员
Long num = redisTemplate.opsForZSet().removeRangeByScore(redisKey, 91, 96);
log.info("移除的成员数:{}", num);
// 获取成员的信息
Setrange = redisTemplate.opsForZSet().range(redisKey, 0, -1);
log.info("获取集合的信息:{}", range);
}
移除的成员数:3
获取集合的信息:[luokaocong]
2.12、ZINTERSTORE、ZUNIONSTORE语法
ZINTERSTORE destination numkeys key [key ...]
ZUNIONSTORE destination numkeys key [key ...]
命令操作
127.0.0.1:6379>zadd zset12 95.0 taojianwen 92.0 tangpeng 91.0 wanglin
(integer) 3
127.0.0.1:6379>zadd zset13 88.0 tangpeng 80.0 wanglin
(integer) 2
127.0.0.1:6379>zinterstore interStore 2 zset12 zset13
(integer) 2
127.0.0.1:6379>zrange interStore 0 -1 withscores
1) "wanglin"
2) "171"
3) "tangpeng"
4) "180"
127.0.0.1:6379>zunionstore unionStore 2 zset12 zset13
(integer) 3
127.0.0.1:6379>zrange unionStore 0 -1 withscores
1) "taojianwen"
2) "95"
3) "wanglin"
4) "171"
5) "tangpeng"
6) "180"
Java操作
@Test
public void zInterStoreAndZUnionStore() {String redisKey = "zSet12";
String redisKey1 = "zSet13";
redisTemplate.delete(redisKey);
redisTemplate.delete(redisKey1);
redisTemplate.opsForZSet().add(redisKey,"taojianwen", 95.0);
redisTemplate.opsForZSet().add(redisKey,"tangpeng", 92.0);
redisTemplate.opsForZSet().add(redisKey,"wanglin", 91.0);
redisTemplate.opsForZSet().add(redisKey1,"tangpeng", 88.0);
redisTemplate.opsForZSet().add(redisKey1,"wanglin", 80.0);
// 计算给定的一个或多个有序集的交集,存储到集合interStore
Long num = redisTemplate.opsForZSet().intersectAndStore(redisKey, redisKey1, "interStore");
log.info("交集的成员数:{}", num);
// 获取成员的信息
Setrange = redisTemplate.opsForZSet().range("interStore", 0, -1);
log.info("获取集合的信息:{}", range);
// 计算给定的一个或多个有序集的并集,存储到集合unionStore
Long num1 = redisTemplate.opsForZSet().unionAndStore(redisKey, redisKey1, "unionStore");
log.info("并集的成员数:{}", num1);
// 获取成员的信息
Setrange1 = redisTemplate.opsForZSet().range("unionStore", 0, -1);
log.info("获取集合的信息:{}", range1);
}
交集的成员数:2
获取集合的信息:[wanglin, tangpeng]
并集的成员数:3
获取集合的信息:[taojianwen, wanglin, tangpeng]
2.13、ZRANGEBYLEX、ZREVRANGEBYLEX语法
ZRANGEBYLEX KEY_NAME min max
ZREVRANGEBYLEX KEY_NAME max min
命令操作
127.0.0.1:6379>zadd zset14 100 aa 100 ab 100 bb 100 bc 100 cc 100 cd 100 dd
(integer) 7
127.0.0.1:6379>zrangebylex zset14 - +
1) "aa"
2) "ab"
3) "bb"
4) "bc"
5) "cc"
6) "cd"
7) "dd"
127.0.0.1:6379>zrangebylex zset14 (bb [cd
1) "bc"
2) "cc"
3) "cd"
127.0.0.1:6379>zrevrangebylex zset14 [cd (bb
1) "cd"
2) "cc"
3) "bc"
Java操作
@Test
public void zRangeByLexAndZRevRangeByLex() {String redisKey = "zSet14";
redisTemplate.delete(redisKey);
redisTemplate.opsForZSet().add(redisKey, "aa", 100);
redisTemplate.opsForZSet().add(redisKey, "ab", 100);
redisTemplate.opsForZSet().add(redisKey, "bb", 100);
redisTemplate.opsForZSet().add(redisKey, "bc", 100);
redisTemplate.opsForZSet().add(redisKey, "cc", 100);
redisTemplate.opsForZSet().add(redisKey, "cd", 100);
redisTemplate.opsForZSet().add(redisKey, "dd", 100);
RedisZSetCommands.Range range = new RedisZSetCommands.Range();
range.gt("bb");
range.lte("cd");
// 字典序指定范围内的成员
Setset1 = redisTemplate.opsForZSet().rangeByLex(redisKey, range);
log.info("字典序指定范围内的成员:{}", set1);
Setset2 = redisTemplate.opsForZSet().reverseRangeByLex(redisKey, range);
log.info("字典序指定范围内的成员:{}", set2);
}
结果未生效,官方文档说明暂不支持该命令
2.14、ZLEXCOUNT语法
ZLEXCOUNT KEY_NAME MIN MAX
命令操作
127.0.0.1:6379>zadd zset15 100 aa 100 ab 100 bb 100 bc 100 cc 100 cd 100 dd
(integer) 7
127.0.0.1:6379>zlexcount zset15 (bb [cd
(integer) 3
Java操作
@Test
public void zLexCount() {String redisKey = "zSet15";
redisTemplate.delete(redisKey);
redisTemplate.opsForZSet().add(redisKey, "aa", 100);
redisTemplate.opsForZSet().add(redisKey, "ab", 100);
redisTemplate.opsForZSet().add(redisKey, "bb", 100);
redisTemplate.opsForZSet().add(redisKey, "bc", 100);
redisTemplate.opsForZSet().add(redisKey, "cc", 100);
redisTemplate.opsForZSet().add(redisKey, "cd", 100);
redisTemplate.opsForZSet().add(redisKey, "dd", 100);
RedisZSetCommands.Range range = new RedisZSetCommands.Range();
range.gt("bb");
range.lte("cd");
// 计算指定字典区间内成员数量
Long count = redisTemplate.opsForZSet().lexCount(redisKey, range);
log.info("计算指定字典区间内成员数量:{}", count);
}
结果未生效,官方文档说明暂不支持该命令(见上图)
2.15、ZREMRANGEBYLEX语法
ZREMRANGEBYLEX KEY_NAME min max
命令操作
127.0.0.1:6379>zadd zset16 100 aa 100 ab 100 bb 100 bc 100 cc 100 cd 100 dd
(integer) 7
127.0.0.1:6379>zremrangebylex zset16 (bb [cd
(integer) 3
127.0.0.1:6379>zrange zset16 0 -1 withscores
1) "aa"
2) "100"
3) "ab"
4) "100"
5) "bb"
6) "100"
7) "dd"
8) "100"
Java操作
@Test
public void zRemRangeByLex() {String redisKey = "zSet16";
redisTemplate.delete(redisKey);
redisTemplate.opsForZSet().add(redisKey, "aa", 100);
redisTemplate.opsForZSet().add(redisKey, "ab", 100);
redisTemplate.opsForZSet().add(redisKey, "bb", 100);
redisTemplate.opsForZSet().add(redisKey, "bc", 100);
redisTemplate.opsForZSet().add(redisKey, "cc", 100);
redisTemplate.opsForZSet().add(redisKey, "cd", 100);
redisTemplate.opsForZSet().add(redisKey, "dd", 100);
RedisZSetCommands.Range range = new RedisZSetCommands.Range();
range.gt("bb");
range.lte("cd");
// 移除有序集合中给定的字典区间的所有成员
Long count = redisTemplate.opsForZSet().removeRangeByLex(redisKey, range);
log.info("移除有序集合中给定的字典区间的所有成员:{}", count);
}
结果未生效,官方文档说明暂不支持该命令(见上图)
2.16、ZPOPMAX、ZPOPMIN语法
ZPOPMAX KEY_NAME [COUNT]
ZPOPMIN KEY_NAME [COUNT]
命令操作
127.0.0.1:6379>zadd zset17 95.0 taojianwen 92.0 tangpeng 91.0 wanglin 90.0 luokaocong
(integer) 4
127.0.0.1:6379>zpopmax zset17 2
1) "taojianwen"
2) "95"
3) "tangpeng"
4) "92"
127.0.0.1:6379>zpopmin zset17 2
1) "luokaocong"
2) "90"
3) "wanglin"
4) "91"
Java操作
@Test
public void zPopMaXAndZPopMin() {String redisKey = "zSet17";
redisTemplate.delete(redisKey);
// 添加多个成员
Set>tuple = new HashSet<>();
tuple.add(new DefaultTypedTuple<>("taojianwen", 95.0));
tuple.add(new DefaultTypedTuple<>("tangpeng", 92.0));
tuple.add(new DefaultTypedTuple<>("wanglin", 91.0));
tuple.add(new DefaultTypedTuple<>("luokaocong", 90.0));
redisTemplate.opsForZSet().add(redisKey, tuple);
// 弹出分数最高的两个成员
Set>typedTuples1 = redisTemplate.opsForZSet().popMax(redisKey, 2);
log.info("弹出分数最高的两个成员:{}", typedTuples1);
// 弹出分数最低的两个成员
Set>typedTuples2 = redisTemplate.opsForZSet().popMin(redisKey, 2);
log.info("弹出分数最低的两个成员:{}", typedTuples2);
}
弹出分数最高的两个成员:[DefaultTypedTuple [score=95.0, value=taojianwen], DefaultTypedTuple [score=92.0, value=tangpeng]]
弹出分数最低的两个成员:[DefaultTypedTuple [score=90.0, value=luokaocong], DefaultTypedTuple [score=91.0, value=wanglin]]
2.17、BZPOPMAX、BZPOPMIN语法
命令操作
127.0.0.1:6379>zadd zset18 95.0 taojianwen 92.0 tangpeng 91.0 wanglin 90.0 luokaocong
(integer) 4
127.0.0.1:6379>bzpopmax zset18 10
1) "zset18"
2) "taojianwen"
3) "95"
127.0.0.1:6379>bzpopmin zset18 10
1) "zset18"
2) "luokaocong"
3) "90"
Java操作
@Test
public void zBPopMaXAndZBPopMin() {String redisKey = "zSet18";
redisTemplate.delete(redisKey);
// 添加多个成员
Set>tuple = new HashSet<>();
tuple.add(new DefaultTypedTuple<>("taojianwen", 95.0));
tuple.add(new DefaultTypedTuple<>("tangpeng", 92.0));
tuple.add(new DefaultTypedTuple<>("wanglin", 91.0));
tuple.add(new DefaultTypedTuple<>("luokaocong", 90.0));
redisTemplate.opsForZSet().add(redisKey, tuple);
// 阻塞式弹出分数最高的成员,如果没有,则直到超时
ZSetOperations.TypedTupleobjectTypedTuple = redisTemplate.opsForZSet().popMax(redisKey, 10, TimeUnit.SECONDS);
log.info("弹出分数最高的成员:{}", objectTypedTuple);
// 阻塞式弹出分数最低的成员,如果没有,则直到超时
ZSetOperations.TypedTupleobjectTypedTuple2 = redisTemplate.opsForZSet().popMin(redisKey, 10, TimeUnit.SECONDS);
log.info("弹出分数最低的成员:{}", objectTypedTuple2);
}
弹出分数最高的成员:DefaultTypedTuple [score=95.0, value=taojianwen]
弹出分数最低的成员:DefaultTypedTuple [score=90.0, value=luokaocong]
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧