当前位置:网站首页>Redistemplate common collection instructions opsforzset (VI)

Redistemplate common collection instructions opsforzset (VI)

2022-07-06 21:21:00 Archie_ java

The basic configuration has been introduced in the previous 《RedisTemplate Instructions for common sets ( One )]》 Has been introduced in , Now let's introduce it directly opsForZSet() Use of methods :

1、add(K key, V value, double score)

Add the element to the variable and specify the score of the element .

redisTemplate.opsForZSet().add("zSetValue","A",1);
redisTemplate.opsForZSet().add("zSetValue","B",3);
redisTemplate.opsForZSet().add("zSetValue","C",2);
redisTemplate.opsForZSet().add("zSetValue","D",5);

2、range(K key, long start, long end)

​ Get the element of the specified interval of the variable .

Set zSetValue = redisTemplate.opsForZSet().range("zSetValue",0,-1);
System.out.println(" adopt range(K key, long start, long end) Method to get the element of the specified interval :" + zSetValue);

3、rangeByLex(K key, RedisZSetCommands.Range range)

Used to obtain information that meets the requirements of non - score Sort value of . This sort can only be used if there are the same scores , If there are different scores, the return value is uncertain .

RedisZSetCommands.Range range = new RedisZSetCommands.Range();
//range.gt("A");
range.lt("D");
zSetValue = redisTemplate.opsForZSet().rangeByLex("zSetValue", range);
System.out.println(" adopt rangeByLex(K key, RedisZSetCommands.Range range) Method to obtain a non score Sort value element of :" + zSetValue);

4、rangeByLex(K key, RedisZSetCommands.Range range, RedisZSetCommands.Limit limit)

​ Used to obtain information that meets the requirements of non - score Set the length sorting value starting from the subscript .

RedisZSetCommands.Limit limit = new RedisZSetCommands.Limit();
limit.count(2);
// Starting subscript is 0
limit.offset(1);
zSetValue = redisTemplate.opsForZSet().rangeByLex("zSetValue", range,limit);
System.out.println(" adopt rangeByLex(K key, RedisZSetCommands.Range range, RedisZSetCommands.Limit limit) Method to obtain a non score Sort value element of :" + zSetValue);

5、add(K key, Set<ZSetOperations.TypedTuple> tuples)

adopt TypedTuple How to add data .

ZSetOperations.TypedTuple<Object> typedTuple1 = new DefaultTypedTuple<Object>("E",6.0);
ZSetOperations.TypedTuple<Object> typedTuple2 = new DefaultTypedTuple<Object>("F",7.0);
ZSetOperations.TypedTuple<Object> typedTuple3 = new DefaultTypedTuple<Object>("G",5.0);
Set<ZSetOperations.TypedTuple<Object>> typedTupleSet = new HashSet<ZSetOperations.TypedTuple<Object>>();
typedTupleSet.add(typedTuple1);
typedTupleSet.add(typedTuple2);
typedTupleSet.add(typedTuple3);
redisTemplate.opsForZSet().add("typedTupleSet",typedTupleSet);
zSetValue = redisTemplate.opsForZSet().range("typedTupleSet",0,-1);
System.out.println(" adopt add(K key, Set<ZSetOperations.TypedTuple<V>> tuples) Method add element :" + zSetValue);

6、rangeByScore(K key, double min, double max)

By setup score Get interval value .

zSetValue = redisTemplate.opsForZSet().rangeByScore("zSetValue",1,2);
System.out.println(" adopt rangeByScore(K key, double min, double max) Method according to the set score Get interval value :" + zSetValue);

7、rangeByScore(K key, double min, double max,long offset, long count)

By setup score Get the interval value and get the final value from the given subscript and given length .

zSetValue = redisTemplate.opsForZSet().rangeByScore("zSetValue",1,5,1,3);
System.out.println(" adopt rangeByScore(K key, double min, double max, long offset, long count) Method according to the set score Get interval value :" + zSetValue);

8、rangeWithScores(K key, long start, long end)

obtain RedisZSetCommands.Tuples The interval value of .

Set<ZSetOperations.TypedTuple<Object>> typedTupleSet = redisTemplate.opsForZSet().rangeWithScores("typedTupleSet",1,3);
Iterator<ZSetOperations.TypedTuple<Object>> iterator = typedTupleSet.iterator();
while (iterator.hasNext()){
    
    ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
    Object value = typedTuple.getValue();
    double score = typedTuple.getScore();
    System.out.println(" adopt rangeWithScores(K key, long start, long end) Method to get RedisZSetCommands.Tuples The interval value of :" + value + "---->" + score );
}

9、rangeByScoreWithScores(K key, double min, double max)

obtain RedisZSetCommands.Tuples The interval value of passes through the score .

Set<ZSetOperations.TypedTuple<Object>> typedTupleSet = redisTemplate.opsForZSet().rangeByScoreWithScores("typedTupleSet",5,8);
iterator = typedTupleSet.iterator();
while (iterator.hasNext()){
    
    ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
    Object value = typedTuple.getValue();
    double score = typedTuple.getScore();
    System.out.println(" adopt rangeByScoreWithScores(K key, double min, double max) Method to get RedisZSetCommands.Tuples The interval value of passes through the score :" + value + "---->" + score );
}

10、rangeByScoreWithScores(K key, double min, double max, long offset, long count)

obtain RedisZSetCommands.Tuples The interval value of gets the final value from the given subscript and given length through the score .

Set<ZSetOperations.TypedTuple<Object>> typedTupleSet = redisTemplate.opsForZSet().rangeByScoreWithScores("typedTupleSet",5,8,1,1);
iterator = typedTupleSet.iterator();
while (iterator.hasNext()){
    
    ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
    Object value = typedTuple.getValue();
    double score = typedTuple.getScore();
    System.out.println(" adopt rangeByScoreWithScores(K key, double min, double max, long offset, long count) Method to get RedisZSetCommands.Tuples The interval value of gets the final value from the given subscript and given length through the score :" + value + "---->" + score );
}

11、count(K key, double min, double max)

Get the number of interval values .

long count = redisTemplate.opsForZSet().count("zSetValue",1,5);
System.out.println(" adopt count(K key, double min, double max) Method to obtain the number of interval values :" + count);

12、rank(K key, Object o)

Get the index of the element in the variable , The starting position of the subscript is 0.

long index = redisTemplate.opsForZSet().rank("zSetValue","B");
System.out.println(" adopt rank(K key, Object o) Method to get the index of the element in the variable :" + index);

13、scan(K key, ScanOptions options)

​ Match to get key value pairs ,ScanOptions.NONE To get all key value pairs ;ScanOptions.scanOptions().match(“C”).build() Match to get the key map1 The key/value pair , No fuzzy matching .

//Cursor<Object> cursor = redisTemplate.opsForSet().scan("setValue", ScanOptions.NONE);
Cursor<ZSetOperations.TypedTuple<Object>> cursor = redisTemplate.opsForZSet().scan("zSetValue", ScanOptions.NONE);
while (cursor.hasNext()){
    
    ZSetOperations.TypedTuple<Object> typedTuple = cursor.next();
    System.out.println(" adopt scan(K key, ScanOptions options) Method to get the matching element :" + typedTuple.getValue() + "--->" + typedTuple.getScore());
}

14、score(K key, Object o)

Get the score of the element .

double score = redisTemplate.opsForZSet().score("zSetValue","B");
System.out.println(" adopt score(K key, Object o) Method to get the score of the element :" + score);

15、zCard(K key)

Get the number of elements in the variable .

long zCard = redisTemplate.opsForZSet().zCard("zSetValue");
System.out.println(" adopt zCard(K key) Method to get the length of the variable :" + zCard);

16、incrementScore(K key, V value, double delta)

Modify the score of the element in the variable .

double incrementScore = redisTemplate.opsForZSet().incrementScore("zSetValue","C",5);
System.out.print(" adopt incrementScore(K key, V value, double delta) Method to modify the score of the element in the variable :" + incrementScore);
score = redisTemplate.opsForZSet().score("zSetValue","C");
System.out.print(", Get the score of the element after modification :" + score);
zSetValue = redisTemplate.opsForZSet().range("zSetValue",0,-1);
System.out.println(", Elements sorted after modification :" + zSetValue);

17、reverseRange(K key, long start, long end)

Arranges the elements of the specified interval in reverse order of the index .

zSetValue = redisTemplate.opsForZSet().reverseRange("zSetValue",0,-1);
System.out.println(" adopt reverseRange(K key, long start, long end) Method to arrange elements in reverse order :" + zSetValue);

18、reverseRangeByScore(K key, double min, double max)

Arrange the elements of the specified score range in reverse order .

zSetValue = redisTemplate.opsForZSet().reverseRangeByScore("zSetValue",1,5);
System.out.println(" adopt reverseRangeByScore(K key, double min, double max) Method to arrange the elements of the specified score interval in reverse order :" + zSetValue);

19、reverseRangeByScore(K key, double min, double max, long offset, long count)

Arrange the elements in reverse order from a given subscript and a given length score interval .

zSetValue = redisTemplate.opsForZSet().reverseRangeByScore("zSetValue",1,5,1,2);
System.out.println(" adopt reverseRangeByScore(K key, double min, double max, long offset, long count) Methods arrange the elements in reverse order from a given subscript and a given length score interval :" + zSetValue);

20、reverseRangeByScoreWithScores(K key, double min, double max)

Sort in reverse order to get RedisZSetCommands.Tuples The score interval value of .

Set<ZSetOperations.TypedTuple<Object>> typedTupleSet = redisTemplate.opsForZSet().reverseRangeByScoreWithScores("zSetValue",1,5);
iterator = typedTupleSet.iterator();
while (iterator.hasNext()){
    
    ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
    Object value = typedTuple.getValue();
    double score1 = typedTuple.getScore();
    System.out.println(" adopt reverseRangeByScoreWithScores(K key, double min, double max) Methods reverse order to get RedisZSetCommands.Tuples The interval value of :" + value + "---->" + score1 );
}

21、reverseRangeByScoreWithScores(K key, double min, double max, long offset, long count)

Sort in reverse order to get RedisZSetCommands.Tuples The interval value from a given subscript and a given length score .

Set<ZSetOperations.TypedTuple<Object>> typedTupleSet = redisTemplate.opsForZSet().reverseRangeByScoreWithScores("zSetValue",1,5,1,2);
iterator = typedTupleSet.iterator();
while (iterator.hasNext()){
    
    ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
    Object value = typedTuple.getValue();
    double score1 = typedTuple.getScore();
    System.out.println(" adopt reverseRangeByScoreWithScores(K key, double min, double max, long offset, long count) Methods reverse order to get RedisZSetCommands.Tuples From the given subscript and the given length interval value :" + value + "---->" + score1 );
}

22、reverseRangeWithScores(K key, long start, long end)

Index the interval values in reverse order .

Set<ZSetOperations.TypedTuple<Object>> typedTupleSet = redisTemplate.opsForZSet().reverseRangeWithScores("zSetValue",1,5);
iterator = typedTupleSet.iterator();
while (iterator.hasNext()){
    
    ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
    Object value = typedTuple.getValue();
    double score1 = typedTuple.getScore();
    System.out.println(" adopt reverseRangeWithScores(K key, long start, long end) Methods index the interval values in reverse order :" + value + "----->" + score1);
}

23、reverseRank(K key, Object o)

Get the index value in reverse order .

long reverseRank = redisTemplate.opsForZSet().reverseRank("zSetValue","B");
System.out.println(" adopt reverseRank(K key, Object o) Get the index value in reverse order :" + reverseRank);

24、intersectAndStore(K key, K otherKey, K destKey)

obtain 2 The intersection of variables is stored in the 3 In a variable .

redisTemplate.opsForZSet().intersectAndStore("zSetValue","typedTupleSet","intersectSet");
zSetValue = redisTemplate.opsForZSet().range("intersectSet",0,-1);
System.out.println(" adopt intersectAndStore(K key, K otherKey, K destKey) Method to get 2 The intersection of variables is stored in the 3 In a variable :" + zSetValue);

25、intersectAndStore(K key, Collection otherKeys, K destKey)

Get the intersection of multiple variables and store it in the 3 In a variable .

List list = new ArrayList();
list.add("typedTupleSet");
redisTemplate.opsForZSet().intersectAndStore("zSetValue",list,"intersectListSet");
zSetValue = redisTemplate.opsForZSet().range("intersectListSet",0,-1);
System.out.println(" adopt intersectAndStore(K key, Collection<K> otherKeys, K destKey) Method to obtain the intersection of multiple variables and store it in the 3 In a variable :" + zSetValue);

26、unionAndStore(K key, K otherKey, K destKey)

obtain 2 The set of variables is stored in the 3 In a variable .

redisTemplate.opsForZSet().unionAndStore("zSetValue","typedTupleSet","unionSet");
zSetValue = redisTemplate.opsForZSet().range("unionSet",0,-1);
System.out.println(" adopt unionAndStore(K key, K otherKey, K destKey) Method to get 2 The intersection of variables is stored in the 3 In a variable :" + zSetValue);

27、unionAndStore(K key, Collection otherKeys, K destKey)

Get the collection of multiple variables and store it in the 3 In a variable .

redisTemplate.opsForZSet().unionAndStore("zSetValue",list,"unionListSet");
zSetValue = redisTemplate.opsForZSet().range("unionListSet",0,-1);
System.out.println(" adopt unionAndStore(K key, Collection<K> otherKeys, K destKey) Method to obtain the intersection of multiple variables and store it in the 3 In a variable :" + zSetValue);

28、remove(K key, Object… values)

Remove elements in batches according to element values .

long removeCount = redisTemplate.opsForZSet().remove("unionListSet","A","B");
zSetValue = redisTemplate.opsForZSet().range("unionListSet",0,-1);
System.out.print(" adopt remove(K key, Object... values) Method to remove the number of elements :" + removeCount);
System.out.println(", The remaining elements after removal :" + zSetValue);

29、removeRangeByScore(K key, double min, double max)

Remove the interval element according to the score .

removeCount = redisTemplate.opsForZSet().removeRangeByScore("unionListSet",3,5);
zSetValue = redisTemplate.opsForZSet().range("unionListSet",0,-1);
System.out.print(" adopt removeRangeByScore(K key, double min, double max) Method to remove the number of elements :" + removeCount);
System.out.println(", The remaining elements after removal :" + zSetValue);

30、removeRange(K key, long start, long end)

​ Remove the interval element according to the index value .

removeCount = redisTemplate.opsForZSet().removeRange("unionListSet",3,5);
zSetValue = redisTemplate.opsForZSet().range("unionListSet",0,-1);
System.out.print(" adopt removeRange(K key, long start, long end) Method to remove the number of elements :" + removeCount);
System.out.println(", The remaining elements after removal :" + zSetValue);

​ Here it is ,RedisTemplate.java Class related collection operations are introduced . Students who need to download the source code can download it in the file I uploaded , It can also be in https://github.com/422518490/springRedisTest.git This github Download above , At the same time, the document description is provided in the uploaded file .

原网站

版权声明
本文为[Archie_ java]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131125013090.html