The data structure Redis ZSet

Redis ordered set of unordered collections and also as a collection of string type elements, and does not allow duplicate members.
The difference is that a double score will be associated with each type of element. It is to redis from small to large order of collection of member passing score.
Members of the ordered set is unique, but the score (score) it can be repeated.
interface ZSetOperations public <K, V>
ZSetOperations provides a series of methods on the ordered set of operations:

  • Boolean add (K key, V value , double score);
    add an ordered collection exists is false, it is true is not present
 
Use: System.out.println (template.opsForZSet () add ( "zset1", "zset1", 1.0).); 
Result: true

 

  • Long add (K key, Set < TypedTuple <V >> tuples);
    add an ordered collection
 
Copy the code
使用:ZSetOperations.TypedTuple<Object> objectTypedTuple1 = new DefaultTypedTuple<Object>("zset-5",9.6);
        ZSetOperations.TypedTuple<Object> objectTypedTuple2 = new DefaultTypedTuple<Object>("zset-6",9.9);
        Set<ZSetOperations.TypedTuple<Object>> tuples = new HashSet<ZSetOperations.TypedTuple<Object>>();
        tuples.add(objectTypedTuple1);
        tuples.add(objectTypedTuple2);
        System.out.println(template.opsForZSet().add("zset1",tuples));
        System.out.println(template.opsForZSet().range("zset1",0,-1));
结果:[zset-1, zset-2, zset-3, zset-4, zset-5, zset-6]
Copy the code

 

  • Long remove (K key, Object ... values);
    removing one or more elements from the ordered sets
 
使用:System.out.println(template.opsForZSet().range("zset1",0,-1));
        System.out.println(template.opsForZSet().remove("zset1","zset-6"));
        System.out.println(template.opsForZSet().range("zset1",0,-1));
结果:[zset-1, zset-2, zset-3, zset-4, zset-5, zset-6]
1
[zset-1, zset-2, zset-3, zset-4, zset-5]

 

  • ; Double incrementScore (K key, V value, double delta)
    value of the score values of the elements increases, and the increase in return
 
Use: System.out.println (template.opsForZSet () incrementScore ( "zset1", "zset1", 1.1).); // original 1.1 
Results: 2.2

 

  • Long rank (K key, Object o );
    Returns the specified member rank ordered set, wherein the ordered set point value is incremented by members (small to large) order
 
Use: System.out.println (. Template.opsForZSet () Range ( "zset1", 0, -1)); 
        . System.out.println (template.opsForZSet () Rank ( "zset1", "2-zset" )); 
results: [zset-2,. 1-zset,. 3-zset,-zset. 4,. 5-zset] 
0 // show ranking

 

  • Long reverseRank (K key, Object o );
    Returns the specified rank ordered set members, wherein the members of the ordered set point value is decremented by (descending) order
 
Use: System.out.println (template.opsForZSet () Range ( "zset1", 0, -1).); 
        System.out.println (template.opsForZSet () reverseRank ( "zset1", "zset-2." )); 
results: [zset-2, zset- 1, zset-3, zset-4, zset-5] 
after 4 // decrement discharged to the fifth

 

  • Set <V> range (K key , long start, long end);
    Returns the index of the interval by an ordered set of members in the synthesis of the specified range, wherein the ordered set point value is incremented by members (small to large) order
 
Use: System.out.println (template.opsForZSet () range ( "zset1", 0, -1).); 
Results: [zset-2, zset1, zset-3, zset-4, zset-5]

 

  • Set <TypedTuple <V >> rangeWithScores ( K key, long start, long end);
    by an index interval returns an ordered set members objects within the specified range of the synthesis, wherein a member of an ordered set point value is incremented by the (small to large) order
 
Copy the code
使用:Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeWithScores("zset1",0,-1);
        Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
        while (iterator.hasNext())
        {
            ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
            System.out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
        }
结果:value:zset-2score:1.2
value:zset-1score:2.2
value:zset-3score:2.3
value:zset-4score:6.6
value:zset-5score:9.6
Copy the code

 

  • Set <V> rangeByScore (K key , double min, double max);
    by fractional return an ordered set of members in the specified range, wherein the ordered set point value is incremented by members (small to large) order
 
Use: System.out.println (template.opsForZSet () rangeByScore ( "zset1", 0,5).); 
Results: [zset-2, zset1, zset-3]

 

  • Set <TypedTuple <V >> rangeByScoreWithScores ( K key, double min, double max);
    by fractional return an ordered set of objects within a specified range members, wherein the members of the ordered set point value is incremented by (small to large) order
 
Copy the code
使用:Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeByScoreWithScores("zset1",0,5);
        Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
        while (iterator.hasNext())
        {
            ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
            System.out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
        }
结果:value:zset-2score:1.2
value:zset-1score:2.2
value:zset-3score:2.3
Copy the code

 

  • Set <V> rangeByScore (K key , double min, double max, long offset, long count);
    by fractional return an ordered set of members in the specified range, and in the index range, wherein the ordered set point value is incremented by members (small to large) order
 
使用: System.out.println(template.opsForZSet().rangeByScore("zset1",0,5));
    System.out.println(template.opsForZSet().rangeByScore("zset1",0,5,1,2));
结果:[zset-2, zset-1, zset-3]
[zset-1, zset-3]

 

  • Set <TypedTuple <V >> rangeByScoreWithScores ( K key, double min, double max, long offset, long count);
    by fractional return an ordered set of objects within a specified range members, and in the index range, wherein the member of an ordered set of points by value is incremented (small to large) order
 
Copy the code
使用:Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeByScoreWithScores("zset1",0,5,1,2);
        Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
        while (iterator.hasNext())
        {
            ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
            System.out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
        }
结果:value:zset-1score:2.2
value:zset-3score:2.3
Copy the code

 

  • Set <V> reverseRange (K key , long start, long end);
    by an index interval returns an ordered set of members in the synthesis of the specified range, wherein the ordered set point value is decremented by members (descending) order
 
使用:System.out.println(template.opsForZSet().reverseRange("zset1",0,-1));
结果:[zset-5, zset-4, zset-3, zset-1, zset-2]

 

  • Set <TypedTuple <V >> reverseRangeWithScores ( K key, long start, long end);
    by an index interval returns an ordered set members objects within the specified range of the synthesis, wherein a member of an ordered set of values in descending order of the score (descending) order
 
Copy the code
使用:Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().reverseRangeWithScores("zset1",0,-1);
        Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
        while (iterator.hasNext())
        {
            ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
            System.out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
        }
结果:value:zset-5score:9.6
value:zset-4score:6.6
value:zset-3score:2.3
value:zset-1score:2.2
value:zset-2score:1.2
Copy the code

 

  • Set<V> reverseRangeByScore(K key, double min, double max);
 
Use: As with the calling method rangeByScore, wherein the ordered set point value is decremented by members (descending) order

 

  • Set<TypedTuple<V>> reverseRangeByScoreWithScores(K key, double min, double max);
 
Use: As with the calling method rangeByScoreWithScores, wherein the ordered set point value is decremented by members (descending) order

 

  • Set<V> reverseRangeByScore(K key, double min, double max, long offset, long count);
 
Use: As with the calling method rangeByScore, wherein the ordered set point value is decremented by members (descending) order

 

  • Set<TypedTuple<V>> reverseRangeByScoreWithScores(K key, double min, double max, long offset, long count);
 
Use: As with the calling method rangeByScoreWithScores, wherein the ordered set point value is decremented by members (descending) order

 

  • Long count (K key, double min , double max);
    Returns the number of members in the specified interval by a fraction of an ordered set of
 
使用:System.out.println(template.opsForZSet().rangeByScore("zset1",0,5));
        System.out.println(template.opsForZSet().count("zset1",0,5));
结果:[zset-2, zset-1, zset-3]
3

 

  • Long size (K key);
    Gets the number of members of an ordered set of internal method call is zCard
 
Use: System.out.println (. Template.opsForZSet () size ( "zset1")); 
Results: 6

 

  • Long zCard (K key);
    Gets the number of members of the ordered set
 
Use: System.out.println (. Template.opsForZSet () zCard ( "zset1")); 
Results: 6

 

  • Double score (K key, Object o );
    Gets score value specified members
 
Use: System.out.println (template.opsForZSet () score ( "zset1", "zset1").); 
Results: 2.2

 

  • Long removeRange (K key, long start , long end);
    removing the specified index member, wherein a member of an ordered set point value is incremented by (small to large) order
 
使用:System.out.println(template.opsForZSet().range("zset2",0,-1));
        System.out.println(template.opsForZSet().removeRange("zset2",1,2));
        System.out.println(template.opsForZSet().range("zset2",0,-1));
结果:[zset-1, zset-2, zset-3, zset-4]
2
[zset-1, zset-4]

 

  • Long removeRangeByScore (K key, double min , double max);
    to remove members from the specified range score worthy
 
Copy the code
使用://System.out.println(template.opsForZSet().add("zset2","zset-1",1.1));
        //System.out.println(template.opsForZSet().add("zset2","zset-2",1.2));
        //System.out.println(template.opsForZSet().add("zset2","zset-3",2.3));
        //System.out.println(template.opsForZSet().add("zset2","zset-4",6.6));
System.out.println(template.opsForZSet().range("zset2",0,-1));
System.out.println(template.opsForZSet().removeRangeByScore("zset2",2,3));
    System.out.println(template.opsForZSet().range("zset2",0,-1));
结果:[zset-1, zset-2, zset-3,zset-4]
1
[zset-1, zset-2, zset-4]
Copy the code

 

  • Long unionAndStore (K key, K otherKey , K destKey);
    calculating a given ordered set and set and stored in a new destKey, the same key will then score value is added
 
Copy the code
使用:System.out.println(template.opsForZSet().add("zzset1","zset-1",1.0));
        System.out.println(template.opsForZSet().add("zzset1","zset-2",2.0));
        System.out.println(template.opsForZSet().add("zzset1","zset-3",3.0));
        System.out.println(template.opsForZSet().add("zzset1","zset-4",6.0));

        System.out.println(template.opsForZSet().add("zzset2","zset-1",1.0));
        System.out.println(template.opsForZSet().add("zzset2","zset-2",2.0));
        System.out.println(template.opsForZSet().add("zzset2","zset-3",3.0));
        System.out.println(template.opsForZSet().add("zzset2","zset-4",6.0));
        System.out.println(template.opsForZSet().add("zzset2","zset-5",7.0));
        System.out.println(template.opsForZSet().unionAndStore("zzset1","zzset2","destZset11"));

        Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeWithScores("destZset11",0,-1);
        Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
        while (iterator.hasNext())
        {
            ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
            System.out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
        }
结果:value:zset-1score:2.0
value:zset-2score:4.0
value:zset-3score:6.0
value:zset-5score:7.0
value:zset-4score:12.0
Copy the code

 

  • Long unionAndStore (K key, Collection < K> otherKeys, K destKey);
    calculating a given ordered set and a plurality of sets, and stored in the new destKey
 
Copy the code
Use: //System.out.println (template.opsForZSet () .add ( "zzset1", "zset-. 1", 1.0)); 
        //System.out.println(template.opsForZSet().add("zzset1 "," zset-2 ", 2.0)); 
        //System.out.println(template.opsForZSet().add("zzset1","zset-3",3.0)); 
        //System.out.println ( . template.opsForZSet () the Add ( "zzset1", "zset-. 4", 6.0)); 
        // 
        //System.out.println(template.opsForZSet().add("zzset2","zset-1 ", 1.0)); 
        //System.out.println(template.opsForZSet().add("zzset2","zset-2",2.0)); 
        //System.out.println(template.opsForZSet () the Add (. "zzset2", ". 3-zset", 3.0)); 
        //System.out.println (template.opsForZSet ().add("zzset2","zset-4",6.0));
        //System.out.println(template.opsForZSet().add("zzset2","zset-5",7.0));

        System.out.println(template.opsForZSet().add("zzset3","zset-1",1.0));
        System.out.println(template.opsForZSet().add("zzset3","zset-2",2.0));
        System.out.println(template.opsForZSet().add("zzset3","zset-3",3.0));
        System.out.println(template.opsForZSet().add("zzset3","zset-4",6.0));
        System.out.println(template.opsForZSet().add("zzset3","zset-5",7.0));

        List<String> stringList = new ArrayList<String>();
        stringList.add("zzset2");
        stringList.add("zzset3");
        System.out.println(template.opsForZSet().unionAndStore("zzset1",stringList,"destZset22"));

        Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeWithScores("destZset22",0,-1);
        Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
        while (iterator.hasNext())
        {
            ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
            System.out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
        }
结果:value:zset-1score:3.0
value:zset-2score:6.0
value:zset-3score:9.0
value:zset-5score:14.0
value:zset-4score:18.0
Copy the code

 

  • Long intersectAndStore (K key, K otherKey , K destKey);
    calculated given one or more of the ordered set of intersections and the result set stored in the new key in the ordered set
 
Copy the code
使用:System.out.println(template.opsForZSet().intersectAndStore("zzset1","zzset2","destZset33"));

        Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeWithScores("destZset33",0,-1);
        Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
        while (iterator.hasNext())
        {
            ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
            System.out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
        }
结果:value:zset-1score:2.0
value:zset-2score:4.0
value:zset-3score:6.0
value:zset-4score:12.0
Copy the code

 

  • Long intersectAndStore (K key, Collection < K> otherKeys, K destKey);
    calculated given one or more of the ordered set of intersections and the result set stored in the new key in the ordered set
 
Copy the code
使用:List<String> stringList = new ArrayList<String>();
        stringList.add("zzset2");
        stringList.add("zzset3");
        System.out.println(template.opsForZSet().intersectAndStore("zzset1",stringList,"destZset44"));

        Set<ZSetOperations.TypedTuple<Object>> tuples = template.opsForZSet().rangeWithScores("destZset44",0,-1);
        Iterator<ZSetOperations.TypedTuple<Object>> iterator = tuples.iterator();
        while (iterator.hasNext())
        {
            ZSetOperations.TypedTuple<Object> typedTuple = iterator.next();
            System.out.println("value:" + typedTuple.getValue() + "score:" + typedTuple.getScore());
Results: value: zset-1score: 3.0
        }
value:zset-2score:6.0
value:zset-3score:9.0
value:zset-4score:18.0
Copy the code

 

  • Cursor<TypedTuple<V>> scan(K key, ScanOptions options);
    遍历zset
 
Copy the code
使用: Cursor<ZSetOperations.TypedTuple<Object>> cursor = template.opsForZSet().scan("zzset1", ScanOptions.NONE);
        while (cursor.hasNext()){
            ZSetOperations.TypedTuple<Object> item = cursor.next();
            System.out.println(item.getValue() + ":" + item.getScore());
        }
结果:zset-1:1.0
zset-2:2.0
zset-3:3.0
zset-4:6.0
Copy the code

 

Note: TimeUnit below java.util.concurrent package is a class that represents a given unit time size
conventional particle
TimeUnit.DAYS // day
TimeUnit.HOURS // hours
TimeUnit.MINUTES // minutes
TimeUnit.SECONDS // sec
TimeUnit.MILLISECONDS // milliseconds

Guess you like

Origin www.cnblogs.com/laoxia/p/11738452.html