ZSet 数据类型操作
zset有序集合,
添加数据
@Test
public void test() {
//增加 按照分数的高低排序
ZSetOperations<String, String> operations = redisTemplate.opsForZSet();
operations.add("name", "zhang", 89);
operations.add("name", "wang", 99);
operations.add("name", "li", 49);
operations.add("name", "sheng", 89);
//增加分数
operations.incrementScore("name", "zhang", 1);
//减小分数
operations.incrementScore("name", "wang", -10);
}
获取
@Test
public void findZsetTest() {
//查询单个
ZSetOperations<String, String> operations = redisTemplate.opsForZSet();
Double score = operations.score("name", "li");
System.out.println(score);
//查询排名 结果从零开始
Long rank = operations.rank("name", "li");
System.out.println(rank);
//查询列表 根据排名查询从0到2的人
Set<String> names = operations.range("name", 0, 2);
for (String name : names) {
System.out.println(name);
}
//根据排名区间查询
Set<TypedTuple<String>> name = operations.rangeWithScores("name", 0, 2);
for (TypedTuple<String> tuple : name) {
System.out.println(tuple.getValue()+":"+tuple.getScore());
}
//更加分数区间查询
Set<String> name1 = operations.rangeByScore("name", 30, 90);
for (String s : name1) {
System.out.println(s);
}
}
统计查询
@Test
public void countTest() {
//统计总数
ZSetOperations<String, String> operations = redisTemplate.opsForZSet();
Long name = operations.zCard("name");
System.out.println(name);
//根据分数区间统计
Long count = operations.count("name", 60, 90);
System.out.println(count);
}
删除
@Test
public void deleteTest() {
ZSetOperations<String, String> operations = redisTemplate.opsForZSet();
//可变数组 删除li
operations.remove("name", "li");
//根据排名区间删除
operations.removeRange("name", 1, 2);
//根据分数区间删除
operations.removeRangeByScore("name", 89, 100);
}