RedisTemplate访问Redis数据结构(五)——ZSet
Redis 有序集合和⽆序集合⼀样也是string类型元素的集合,且不允许重复的成员。不同的是每个元素都会关联⼀个double类型的分数。有序集合的成员是唯⼀的,但分数(score)却可以重复。redis正是通过分数来为集合中的成员进⾏从⼩到⼤的排序。
ZSetOperations提供了⼀系列⽅法对有序集合进⾏操作。⾸先初始化spring⼯⼚获得redisTemplate和opsForZSet
private RedisTemplate<String,Object> redisTemplate;
private ZSetOperations<String, Object> opsForZSet;
@SuppressWarnings("unchecked")
@Before
public void before(){
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("/l");
redisTemplate = (RedisTemplate<String,Object>)Bean("redisTemplate");
opsForZSet = redisTemplate.opsForZSet();
}
Boolean add(K key, V value, double score);
Set< V > range(K key, long start, long end);
@Test
public void testAdd(){
redisTemplate.delete("fan1");
//将值添加到键中的排序集合,如果已存在,则更新其分数。
System.out.println(opsForZSet.add("fan1", "a", 1));//true (这⾥的1.0可以⽤1代替,因为⽤double收参)
ZSetOperations.TypedTuple<Object> objectTypedTuple1 = new DefaultTypedTuple<Object>("b",2.0);//这⾥必须是2.0,因为那边是⽤Double收参
ZSetOperations.TypedTuple<Object> objectTypedTuple2 = new DefaultTypedTuple<Object>("c",3.0);
Set<ZSetOperations.TypedTuple<Object>> tuples = new HashSet<ZSetOperations.TypedTuple<Object>>();
tuples.add(objectTypedTuple1);
tuples.add(objectTypedTuple2);
System.out.println(opsForZSet.add("fan1",tuples));//2
//通过索引区间返回有序集合指定区间内的成员,其中有序集成员按分数值递增(从⼩到⼤)顺序排列
System.out.println(opsForZSet.range("fan1",0,-1));//[a, b, c]
}
Long remove(K key, Object… values);
@Test
public void testRemove(){
redisTemplate.delete("fan2");
opsForZSet.add("fan2", "a", 1);
System.out.println(opsForZSet.range("fan2", 0, -1));//[a]
System.out.println(opsForZSet.range("fan2", 0, -1));//[]
}
public Set< K > keys(K pattern)
Double incrementScore(K key, V value, double delta);
@Test
public void testIncrementScore(){
redisTemplate.delete("fan3");
System.out.println(redisTemplate.keys("fan3"));//[]
//通过增量增加排序集中的元素的分数
System.out.println(opsForZSet.incrementScore("fan3", "a", -1));//-1.0(可见默认技术为0)
System.out.println(redisTemplate.keys("fan3"));//[fan3]
}
Long rank(K key, Object o);
@Test
public void testRank(){
redisTemplate.delete("fan4");
opsForZSet.add("fan4", "a", 1);
opsForZSet.add("fan4", "b", 3);
opsForZSet.add("fan4", "c", 2);
opsForZSet.add("fan4", "d", -1);
System.out.println(opsForZSet.range("fan4", 0, -1));//[d, a, c, b](从⼩到⼤)
//在排序集中确定具有值的元素的索引,并返回其索引(从低到⾼)
System.out.println(opsForZSet.rank("fan4", "b"));//3(从⼩到⼤且从零开始)
}
Long reverseRank(K key, Object o);
public void testReverseRank(){
redisTemplate.delete("fan5");
opsForZSet.add("fan5", "a", 1);
opsForZSet.add("fan5", "b", 3);
opsForZSet.add("fan5", "c", 2);
opsForZSet.add("fan5", "d", -1);
//当从⾼到低时,确定排序集中的值的元素的索引。
System.out.verseRank("fan5", "b"));//0(从⼤到⼩且从零开始)
}
Set< TypedTuple< V >> rangeWithScores(K key, long start, long end);
@Test
public void testRangeWithScores(){
redisTemplate.delete("fan6");
opsForZSet.add("fan6", "a", 1);
opsForZSet.add("fan6", "b", 3);
opsForZSet.add("fan6", "c", 2);
opsForZSet.add("fan6", "d", -1);
//从排序集中获取开始和结束之间的元组(Tuple)。
Set<TypedTuple<Object>> rangeWithScores = opsForZSet.rangeWithScores("fan6", 0  , -1);
Iterator<TypedTuple<Object>> iterator = rangeWithScores.iterator();
while(iterator.hasNext()){
TypedTuple<Object> next = ();
System.out.println("value:"+Value()+" score:"+Score());
/
*
value:d score:-1.0
value:a score:1.0
value:c score:2.0
value:b score:3.0
*/
}
}
Set< V > rangeByScore(K key, double min, double max);
@Test
public void testRangeByScore(){
redisTemplate.delete("fan7");
opsForZSet.add("fan7", "a", 1);
opsForZSet.add("fan7", "b", 3);
opsForZSet.add("fan7", "c", 2);
opsForZSet.add("fan7", "d", -1);
//得到分数在最⼩和最⼤值之间的元素。(从⼩到⼤)
Set<Object> rangeByScore = opsForZSet.rangeByScore("fan7", 1, 2);
System.out.println(rangeByScore);//[a, c]
//从开始到结束的范围内获取元素,其中分数在分类集合的最⼩值和最⼤值之间。
Set<Object> rangeByScore2 = opsForZSet.rangeByScore("fan7", 0, 10, 0, -1);
System.out.println(rangeByScore2);//[a, c, b]
Set<Object> rangeByScore3 = opsForZSet.rangeByScore("fan7", -1, 3, 0, 1);
System.out.println(rangeByScore3);//[d]
}
Set< TypedTuple< V >> rangeByScoreWithScores(K key, double min, double max);
Set< TypedTuple< V >> rangeByScoreWithScores(K key, double min, double max, long offset, long count);
@Test
public void testRangeByScoreWithScores(){
redisTemplate.delete("fan8");
opsForZSet.add("fan8", "a", 1);
opsForZSet.add("fan8", "b", 3);
opsForZSet.add("fan8", "c", 2);
opsForZSet.add("fan8", "d", -1);
//得到⼀组元组,其中分数在分类集合的最⼩值和最⼤值之间
Set<TypedTuple<Object>> rangeByScoreWithScores = opsForZSet.rangeByScoreWithScores("fan8", 1, 2);//注意("fan8",2,1)是获取不到数据的        Iterator<TypedTuple<Object>> iterator = rangeByScoreWithScores.iterator();
while(iterator.hasNext()){
TypedTuple<Object> next = ();
System.out.println("value:"+Value()+" score:"+Score());
/*
value:a score:1.0
value:c score:2.0
*/
}
//从开始到结束的范围内获取⼀组元组,其中分数在分类集中的最⼩值和最⼤值之间。
Set<TypedTuple<Object>> rangeByScoreWithScores2 = opsForZSet.rangeByScoreWithScores("fan8", 1, 2, 1, 2);
Iterator<TypedTuple<Object>> iterator2 = rangeByScoreWithScores2.iterator();
while(iterator2.hasNext()){
value:c score:2.0
*/
}
}
Set reverseRange(K key, long start, long end);
Set< TypedTuple< V >> reverseRangeWithScores(K key, long start, long end);
Set< V > reverseRangeByScore(K key, double min, double max);
Set< TypedTuple< V >> reverseRangeByScoreWithScores(K key, double min, double max);
Set< V > reverseRangeByScore(K key, double min, double max, long offset, long count);
Set< TypedTuple< V >> reverseRangeByScoreWithScores(K key, double min, double max, long offset, long count);
@Test
public void testReverseRange(){
redisTemplate.delete("fan9");
opsForZSet.add("fan9", "a", 1);
opsForZSet.add("fan9", "b", 3);
opsForZSet.add("fan9", "c", 2);
opsForZSet.add("fan9", "d", -1);
//从从⾼到低的排序集中获取从头(start)到尾(end)内的元素。
Set<Object> reverseRange = verseRange("fan9", 0, -1);
System.out.println(reverseRange);//[b, c, a, d]
//从开始(start)到结束(end),从排序从⾼到低的排序集中获取元组的集合
Set<TypedTuple<Object>> reverseRangeWithScores = verseRangeWithScores("fan9", 0, -1);
Iterator<TypedTuple<Object>> iterator = reverseRangeWithScores.iterator();
while(iterator.hasNext()){
TypedTuple<Object> next = ();
System.out.println("value:"+Value()+" score:"+Score());
/*
value:b score:3.0
value:c score:2.0
value:a score:1.0
value:d score:-1.0
*/
}
//从⾼到低的排序集中获取分数在最⼩和最⼤值之间的元素。
Set<Object> reverseRangeByScore = verseRangeByScore("fan9", -1, 2);
System.out.println(reverseRangeByScore);//[c, a, d]
//从开始到结束的范围内获取元素,其中分数在最⼩和最⼤之间,从排序集排序⾼ - >低。
Set<Object> reverseRangeByScore2 = verseRangeByScore("fan9", -1, 2, 2, 3);
System.out.println(reverseRangeByScore2);//[d]
/
/得到⼀组元组,其中分数在最⼩和最⼤之间,从排序从⾼到低
Set<TypedTuple<Object>> reverseRangeByScoreWithScores = verseRangeByScoreWithScores("fan9", -1, 2);
Iterator<TypedTuple<Object>> iterator2 = reverseRangeByScoreWithScores.iterator();
while(iterator2.hasNext()){
TypedTuple<Object> next = ();
System.out.println("value:"+Value()+" score:"+Score());
/*
value:c score:2.0
value:a score:1.0
value:d score:-1.0
*/
}
//从开始到结束的范围内获取⼀组元组,其中分数在最⼩和最⼤之间,从排序集排序⾼ - >低。
Set<TypedTuple<Object>> reverseRangeByScoreWithScores2 = verseRangeByScoreWithScores("fan9", -1, 2, 1, 3);        Iterator<TypedTuple<Object>> iterator3 = reverseRangeByScoreWithScores2.iterator();
while(iterator3.hasNext()){
TypedTuple<Object> next = ();
System.out.println("value:"+Value()+" score:"+Score());
/*
value:a score:1.0
value:d score:-1.0
*/
}
}
Long count(K key, double min, double max);
@Test
public void testCount(){
redisTemplate.delete("fan10");
opsForZSet.add("fan10", "a", 1);
opsForZSet.add("fan10", "b", 3);
//计算排序集中在最⼩和最⼤分数之间的元素数。
Long count = unt("fan10", -1, 2);
System.out.println(count);//3
}
Long size(K key);
Long zCard(K key);
@Test
public void testSizeAndZCard(){
redisTemplate.delete("fan11");
opsForZSet.add("fan11", "a", 1);
opsForZSet.add("fan11", "b", 3);
opsForZSet.add("fan11", "c", 2);
opsForZSet.add("fan11", "d", -1);
//返回使⽤给定键存储的排序集的元素数(其实size()底层就是调⽤的zCard())
Long size = opsForZSet.size("fan11");
System.out.println(size);//4
//使⽤键获取排序集的⼤⼩。
Long zCard = opsForZSet.zCard("fan11");
System.out.println(zCard);//4
}
Double score(K key, Object o);
@Test
public void testScore(){
redisTemplate.delete("fan12");
opsForZSet.add("fan12", "a", 1);
opsForZSet.add("fan12", "b", 3);
opsForZSet.add("fan12", "c", 2);
opsForZSet.add("fan12", "d", -1);
redis五种数据结构//使⽤键值从排序集中获取具有值的元素的分数
Double score = opsForZSet.score("fan12", "b");
System.out.println(score);//3.0
}
Long removeRange(K key, long start, long end);
@Test
public void testRemoveRange(){
redisTemplate.delete("fan13");
opsForZSet.add("fan13", "a", 1);
opsForZSet.add("fan13", "b", 3);
opsForZSet.add("fan13", "c", 2);
opsForZSet.add("fan13", "d", -1);
//使⽤键从排序集中删除开始和结束之间范围内的元素
Long removeRange = veRange("fan13", 1, 3);
System.out.println(removeRange);//3
System.out.println(opsForZSet.zCard("fan13"));//1
}
Long removeRangeByScore(K key, double min, double max);
@Test
public void testRemoveRangeByScore(){
redisTemplate.delete("fan13");
opsForZSet.add("fan13", "a", 1);
opsForZSet.add("fan13", "b", 3);
opsForZSet.add("fan13", "c", 2);
opsForZSet.add("fan13", "d", -1);
//使⽤键从排序集中移除最⼩和最⼤值之间的元素
Long removeRangeByScore = veRangeByScore("fan13", 2, 100);        System.out.println(removeRangeByScore);//2
}
Long unionAndStore(K key, K otherKey, K destKey);
Long unionAndStore(K key, Collection< K > otherKeys, K destKey);
@Test
public void testUnionAndStore(){
redisTemplate.delete("fan14");
redisTemplate.delete("fan15");
redisTemplate.delete("fan16");
redisTemplate.delete("fan17");
redisTemplate.delete("fan18");
opsForZSet.add("fan14", "c", 2);
opsForZSet.add("fan14", "d", -1);
opsForZSet.add("fan15", "c", 1);
opsForZSet.add("fan15", "d", 3);
opsForZSet.add("fan15", "e", 2);
opsForZSet.add("fan15", "f", -1);
//在键和其他键上的联合排序集合,并将结果存储在⽬标destIny中(注意相交的元素分数相加)
Long unionAndStore = opsForZSet.unionAndStore("fan14", "fan15", "fan16");
System.out.println(unionAndStore);//6
Set<TypedTuple<Object>> rangeWithScores = opsForZSet.rangeWithScores("fan16", 0, -1);
Iterator<TypedTuple<Object>> iterator = rangeWithScores.iterator();
while(iterator.hasNext()){
TypedTuple<Object> next = ();
System.out.println("value:"+Value()+" score:"+Score());
/*
value:f score:-1.0
value:a score:1.0
value:d score:2.0
value:e score:2.0
value:b score:3.0
value:c score:3.0  可以看出,score相加了
*/
}
opsForZSet.add("fan17", "e", 5);
opsForZSet.add("fan17", "f", -7);
opsForZSet.add("fan17", "g", 31);
opsForZSet.add("fan17", "h", -11);
opsForZSet.add("fan17", "c", -11);
//计算给定的多个有序集的并集,并存储在新的 destKey中
Long unionAndStore2 = opsForZSet.unionAndStore("fan14", Arrays.asList("fan15","fan17"), "fan18");        System.out.println(unionAndStore2);//8
Set<TypedTuple<Object>> rangeWithScores2 = opsForZSet.rangeWithScores("fan18", 0, -1);
Iterator<TypedTuple<Object>> iterator2 = rangeWithScores2.iterator();
while(iterator2.hasNext()){
TypedTuple<Object> next = ();
System.out.println("value:"+Value()+" score:"+Score());
/*
value:h score:-11.0
value:c score:-8.0
value:f score:-8.0
value:a score:1.0
value:d score:2.0
value:b score:3.0
value:e score:7.0
value:g score:31.0
*/
}
}
Long intersectAndStore(K key, K otherKey, K destKey);
Long intersectAndStore(K key, Collection< K > otherKeys, K destKey);
@Test
public void testIntersectAndStore(){
redisTemplate.delete("fan19");
redisTemplate.delete("fan20");
redisTemplate.delete("fan21");
redisTemplate.delete("fan22");
redisTemplate.delete("fan23");
opsForZSet.add("fan19", "a", 1);
opsForZSet.add("fan19", "b", 3);
opsForZSet.add("fan19", "c", 2);
opsForZSet.add("fan19", "d", -1);
opsForZSet.add("fan20", "c", 1);
opsForZSet.add("fan20", "d", 3);
opsForZSet.add("fan20", "e", 8);
opsForZSet.add("fan20", "f", -5);
opsForZSet.add("fan21", "e", 1);
opsForZSet.add("fan21", "f", 3);
opsForZSet.add("fan21", "g", 2);
opsForZSet.add("fan21", "h", -1);
opsForZSet.add("fan21", "c", 9);
//计算给定的⼀个与另⼀个有序集的交集并将结果集存储在新的有序集合 key 中
Long intersectAndStore = opsForZSet.intersectAndStore("fan19", "fan20", "fan22");
System.out.println(intersectAndStore);//2
Set<TypedTuple<Object>> rangeWithScores = opsForZSet.rangeWithScores("fan22", 0, -1);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。