SpringBoot--RedisTemplate details (Hash, Set, ZSet)

Redis's Hash data structure

Redis hash allows users to store multiple key value pairs into a redis key.
public interface HashOperations<H,HK,HV>
HashOperations provides a series of method operations hash:

Initial data:
//template.opsForHash().put("redisHash","name","tom");
        //template.opsForHash().put("redisHash","age",26);
        //template.opsForHash().put("redisHash","class","6");

//Map<String,Object> testMap = new HashMap();
        //testMap.put("name","jack");
        //testMap.put("age",27);
        //testMap.put("class","1");
        //template.opsForHash().putAll("redisHash1",testMap);

Long delete(H key, Object... hashKeys);
Delete the given hash hashKeys

Use: System.out.println(template.opsForHash().delete("redisHash","name"));
        System.out.println(template.opsForHash().entries("redisHash"));
//Result:1
{class=6, age=28.1}
  • Boolean hasKey(H key, Object hashKey);
    Determine if the hash hashKey exists
Use: System.out.println(template.opsForHash().hasKey("redisHash","age"));
        System.out.println(template.opsForHash().hasKey("redisHash","ttt"));
//Result:true
false
  • HV get(H key, Object hashKey);
    Get the value of the given hashKey from the hash in the key
Use: System.out.println(template.opsForHash().get("redisHash","age"));
Results: 26
  • List multiGet(H key, Collection hashKeys);
    Get the value of the given hashKey from the hash
Use: List<Object> kes = new ArrayList<Object>();
        kes.add("name");
        kes.add("age");
        System.out.println(template.opsForHash().multiGet("redisHash",kes));
//Result:[jack, 28.1]
  • Long increment(H key, HK hashKey, long delta);
    Increase hash hashKey value (integer) by given delta
Use: System.out.println(template.opsForHash().get("redisHash","age"));
    System.out.println(template.opsForHash().increment("redisHash","age",1));
//Result:26
27
  • Double increment(H key, HK hashKey, double delta);
    Increase the value of hash hashKey (floating point number) through the given delta
Use: System.out.println(template.opsForHash().get("redisHash","age"));
    System.out.println(template.opsForHash().increment("redisHash","age",1.1));
//Result:27
28.1
  • Set keys(H key);
    Get the key of the hash table corresponding to the key
Use: System.out.println(template.opsForHash().keys("redisHash1"));
//The hash table corresponding to redisHash1 is {class=1, name=jack, age=27}
Result: [name, class, age]
  • Long size(H key);
    Get the size of hash table corresponding to key
Use: System.out.println(template.opsForHash().size("redisHash1"));
//The hash table corresponding to redisHash1 is {class=1, name=jack, age=27}
Results: 3
  • Long size(H key);
    Get the size of hash table corresponding to key
Use: System.out.println(template.opsForHash().size("redisHash1"));
//The hash table corresponding to redisHash1 is {class=1, name=jack, age=27}
Results: 3
  • void putAll(H key, Map<? extends HK, ? extends HV> m);
    Set multiple hash fields provided in m to the hash table corresponding to key
Use: Map<String,Object> testMap = new HashMap();
        testMap.put("name","jack");
        testMap.put("age",27);
        testMap.put("class","1");
        template.opsForHash().putAll("redisHash1",testMap);
        System.out.println(template.opsForHash().entries("redisHash1"));
//Result:{class=1, name=jack, age=27}
  • void put(H key, HK hashKey, HV value);
    Set hash hashKey value
Use: template.opsForHash().put("redisHash","name","tom");
        template.opsForHash().put("redisHash","age",26);
        template.opsForHash().put("redisHash","class","6");
System.out.println(template.opsForHash().entries("redisHash"));
//Result:{age=26, class=6, name=tom}
  • Boolean putIfAbsent(H key, HK hashKey, HV value);
    The hash hashKey value is set only if the hashKey does not exist.
Use: System.out.println(template.opsForHash().putIfAbsent("redisHash","age",30));
System.out.println(template.opsForHash().putIfAbsent("redisHash","kkk","kkk"));
//Result:false
true
  • List values(H key);
    Get the value of the entire hash store according to the key
Use: System.out.println(template.opsForHash().values("redisHash"));
Results: [tom, 26, 6]

Map<HK, HV> entries(H key);
Get the entire hash store based on the key

Use: System.out.println(template.opsForHash().entries("redisHash"));
Results: {age=26, class=6, name=tom}
  • Cursor<Map.Entry<HK, HV>> scan(H key, ScanOptions options);
    Using Cursor to iterate in the hash of key is equivalent to iterator.
Use: Cursor<Map.Entry<Object, Object>> curosr = template.opsForHash().scan("redisHash", ScanOptions.ScanOptions.NONE);
        while(curosr.hasNext()){
            Map.Entry<Object, Object> entry = curosr.next();
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
//Results: age:28.1
class:6
kkk:kkk

Redis Set data structure

Redis's Set is an unordered Set of type string. Collection members are unique, which means that duplicate data cannot appear in the collection.
The collection in Redis is implemented by hash table, so the complexity of adding, deleting and searching is O(1).
public interface SetOperations<K,V>
SetOperations provides a series of operations on unordered Collections:

  • Long add(K key, V... values);
    Add elements in unordered collection, return the number of added elements
    You can also add multiple values directly to add, such as template.opsForSet().add("setTest", "aaa", "bbb")
Use: String[] strarrays = new String[]{"strarr1","sgtarr2"};
        System.out.println(template.opsForSet().add("setTest", strarrays));
//Result:2
  • Long remove(K key, Object... values);
    Remove one or more members of the collection
Use: String[] strarrays = new String[]{"strarr1","sgtarr2"};
System.out.println(template.opsForSet().remove("setTest",strarrays));
//Result:2
  • V pop(K key);
    Remove and return a random element in the collection
Use: System.out.println(template.opsForSet().pop("setTest"));
System.out.println(template.opsForSet().members("setTest"));
//Results: bbb
[aaa, ccc]
  • Boolean move(K key, V value, K destKey);
    Move member elements from the source set to the destination set
Use: template.opsForSet().move("setTest","aaa","setTest2");
        System.out.println(template.opsForSet().members("setTest"));
        System.out.println(template.opsForSet().members("setTest2"));
//Result:[ccc]
[aaa]
  • Long size(K key);
    Size length of unordered collection
Use: System.out.println(template.opsForSet().size("setTest"));
Results: 1
  • Boolean isMember(K key, Object o);
    Determine whether the member element is a member of the set key
Use: System.out.println(template.opsForSet().isMember("setTest","ccc"));
        System.out.println(template.opsForSet().isMember("setTest","asd"));
//Result:true
false
  • Set intersect(K key, K otherKey);
    Intersection of the unordered set corresponding to key and the unordered set corresponding to other key
Use: System.out.println(template.opsForSet().members("setTest"));
        System.out.println(template.opsForSet().members("setTest2"));
        System.out.println(template.opsForSet().intersect("setTest","setTest2"));
//Result:[aaa, ccc]
[aaa]
[aaa]
  • Set intersect(K key, Collection otherKeys);
    Intersection of the unordered set corresponding to key and the unordered set corresponding to multiple other keys
Use: System.out.println(template.opsForSet().members("setTest"));
        System.out.println(template.opsForSet().members("setTest2"));
        System.out.println(template.opsForSet().members("setTest3"));
        List<String> strlist = new ArrayList<String>();
        strlist.add("setTest2");
        strlist.add("setTest3");
        System.out.println(template.opsForSet().intersect("setTest",strlist));
//Result:[aaa, ccc]
[aaa]
[ccc, aaa]
[aaa]
  • Long intersectAndStore(K key, K otherKey, K destKey);
    The intersection of key unordered set and other key unordered set is stored in destKey unordered set
Use: System.out.println("setTest:" + template.opsForSet().members("setTest"));
System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
System.out.println(template.opsForSet().intersectAndStore("setTest","setTest2","destKey1"));
System.out.println(template.opsForSet().members("destKey1"));
//Result: setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
2
[aaa, ccc]
  • Long intersectAndStore(K key, Collection otherKeys, K destKey);
    The intersection of the unordered set corresponding to key and the unordered set corresponding to multiple other keys is stored in the destKey unordered set
Use: System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
        System.out.println("setTest3:" + template.opsForSet().members("setTest3"));
        List<String> strlist = new ArrayList<String>();
        strlist.add("setTest2");
        strlist.add("setTest3");
        System.out.println(template.opsForSet().intersectAndStore("setTest",strlist,"destKey2"));
        System.out.println(template.opsForSet().members("destKey2"));
//Result: setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[ccc, aaa]
2
[aaa, ccc]
  • Set union(K key, K otherKey);
    Union of key unordered set and other key unordered set
Use: System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
        System.out.println(template.opsForSet().union("setTest","setTest2"));
//Result: setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
[ccc, aaa, ddd, bbb]
  • Set union(K key, Collection otherKeys);
    Union of key unordered set and multiple other key unordered sets
Use: System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
        System.out.println("setTest3:" + template.opsForSet().members("setTest3"));
        List<String> strlist = new ArrayList<String>();
        strlist.add("setTest2");
        strlist.add("setTest3");
        System.out.println(template.opsForSet().union("setTest",strlist));
//Result: setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[xxx, ccc, aaa]
[ddd, xxx, bbb, aaa, ccc]
  • Long unionAndStore(K key, K otherKey, K destKey);
    The union of key unordered set and other key unordered set is stored in destKey unordered set
Use: System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
        System.out.println(template.opsForSet().unionAndStore("setTest","setTest2","unionAndStoreTest1"));
        System.out.println("unionAndStoreTest1:" + template.opsForSet().members("unionAndStoreTest1"));
//Result: setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
4
unionAndStoreTest1:[ccc, aaa, ddd, bbb]
  • Long unionAndStore(K key, Collection otherKeys, K destKey);
    The union of key unordered set and multiple other key unordered sets is stored in destKey unordered set
Use: System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
        System.out.println("setTest3:" + template.opsForSet().members("setTest3"));
        List<String> strlist = new ArrayList<String>();
        strlist.add("setTest2");
        strlist.add("setTest3");
        System.out.println(template.opsForSet().unionAndStore("setTest",strlist,"unionAndStoreTest2"));
        System.out.println("unionAndStoreTest2:" + template.opsForSet().members("unionAndStoreTest2"));
//Result: setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[xxx, ccc, aaa]
5
unionAndStoreTest2:[ddd, xxx, bbb, aaa, ccc]
  • Set difference(K key, K otherKey);
    Difference set of key unordered set and other key unordered set
Use: System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
        System.out.println(template.opsForSet().difference("setTest","setTest2"));
//Result: setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
[bbb, ddd]
  • Set difference(K key, Collection otherKeys);
    Key unordered set and difference set of many other key unordered sets
Use: System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
        System.out.println("setTest3:" + template.opsForSet().members("setTest3"));
        List<String> strlist = new ArrayList<String>();
        strlist.add("setTest2");
        strlist.add("setTest3");
        System.out.println(template.opsForSet().difference("setTest",strlist));
//Result: setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[xxx, ccc, aaa]
[bbb, ddd]
  • Long differenceAndStore(K key, K otherKey, K destKey);
    The difference set of key unordered set and other key unordered set is stored in destKey unordered set
Use: System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
        System.out.println(template.opsForSet().differenceAndStore("setTest","setTest2","differenceAndStore1"));
        System.out.println("differenceAndStore1:" + template.opsForSet().members("differenceAndStore1"));
//Result: setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
2
differenceAndStore1:[bbb, ddd]
  • Long differenceAndStore(K key, Collection otherKeys, K destKey);
    The difference set of key unordered set and multiple other key unordered sets is stored in destKey unordered set
Use: System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTest2:" + template.opsForSet().members("setTest2"));
        System.out.println("setTest3:" + template.opsForSet().members("setTest3"));
        List<String> strlist = new ArrayList<String>();
        strlist.add("setTest2");
        strlist.add("setTest3");
        System.out.println(template.opsForSet().differenceAndStore("setTest",strlist,"differenceAndStore2"));
        System.out.println("differenceAndStore2:" + template.opsForSet().members("differenceAndStore2"));
//Result: setTest:[ddd, bbb, aaa, ccc]
setTest2:[ccc, aaa]
setTest3:[xxx, ccc, aaa]
2
differenceAndStore2:[bbb, ddd]
  • Set members(K key);
    Returns all members of the collection
Use: System.out.println(template.opsForSet().members("setTest"));
Results: [ddd, bbb, aaa, ccc]
  • V randomMember(K key);
    Random access to an element in a key unordered set
Use: System.out.println("setTest:" + template.opsForSet().members("setTest"));
        System.out.println("setTestrandomMember:" + template.opsForSet().randomMember("setTest"));
        System.out.println("setTestrandomMember:" + template.opsForSet().randomMember("setTest"));
        System.out.println("setTestrandomMember:" + template.opsForSet().randomMember("setTest"));
        System.out.println("setTestrandomMember:" + template.opsForSet().randomMember("setTest"));
//Result: setTest:[ddd, bbb, aaa, ccc]
setTestrandomMember:aaa
setTestrandomMember:bbb
setTestrandomMember:aaa
setTestrandomMember:ddd
  • Set distinctRandomMembers(K key, long count);
    Get the elements (de duplication) in multiple key unordered sets, and count represents the number
Use: system. Out. Println ("random members:" + template. Opsforset(). Distinct random members ("settest", 5));
Results: random members: [AAA, BBB, DDD, CCC]
  • List randomMembers(K key, long count);
    Get the elements in the unordered collection of multiple key s, and count represents the number
Use: system. Out. Println ("random members:" + template. Opsforset(). Random members ("settest", 5));
Results: random members: [CCC, DDD, DDD, DDD, AAA]
  • Cursor scan(K key, ScanOptions options);
    Traversing set
Use: Cursor<Object> curosr = template.opsForSet().scan("setTest", ScanOptions.NONE);
        while(curosr.hasNext()){
            System.out.println(curosr.next());
        }
//Results: ddd
bbb
aaa
ccc

Redis's ZSet data structure

Redis ordered and unordered collections are also collections of string type elements, and duplicate members are not allowed.
The difference is that each element is associated with a score of type double. redis sorts the members of the collection from small to large by scores.
Members of an ordered set are unique, but scores can be repeated.
public interface ZSetOperations<K,V>
ZSetOperations provides a series of methods to operate on ordered sets:

  • Boolean add(K key, V value, double score);
    Add an ordered set. If it exists, it is false. If it does not exist, it is true
Use: System.out.println(template.opsForZSet().add("zset1","zset-1",1.0));
Result: true
  • Long add(K key, Set<TypedTuple> tuples);
    Add an ordered set
Use: 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));
//Result:[zset-1, zset-2, zset-3, zset-4, zset-5, zset-6]
  • Long remove(K key, Object... values);
    Remove one or more elements from an ordered collection
Use: 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));
//Result:[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);
    Increase the score value of the element and return the increased value
Use: System.out.println(template.opsForZSet().incrementScore("zset1","zset-1",1.1)); / / formerly 1.1
 Results: 2.2
  • Long rank(K key, Object o);
    Returns the ranking of the specified members in an ordered set, in which the members of the ordered set are arranged in ascending (small to large) Order
Use: System.out.println(template.opsForZSet().range("zset1",0,-1));
        System.out.println(template.opsForZSet().rank("zset1","zset-2"));
//Result:[zset-2, zset-1, zset-3, zset-4, zset-5]
0   //Show No.1
  • Long reverseRank(K key, Object o);
    Returns the ranking of the specified members in an ordered set, where the members of the ordered set are arranged in descending order (from large to small) according to the fractional value
Use: System.out.println(template.opsForZSet().range("zset1",0,-1));
        System.out.println(template.opsForZSet().reverseRank("zset1","zset-2"));
//Result:[zset-2, zset-1, zset-3, zset-4, zset-5]
4 //It's down to number five
  • Set range(K key, long start, long end);
    The members in the specified interval are synthesized by returning the ordered set from the index interval, and the members of the ordered set are arranged in ascending (small to large) Order
Use: System.out.println(template.opsForZSet().range("zset1",0,-1));
Results: [zset-2, zset-1, zset-3, zset-4, zset-5]
  • Set<TypedTuple> rangeWithScores(K key, long start, long end);
    Returns the ordered set through the index interval to synthesize the member objects in the specified interval, where the members of the ordered set are arranged in the order of increasing (from small to large) by the fractional value
Use: 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());
        }
//Result: 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
  • et rangeByScore(K key, double min, double max);
    Returns the members of an ordered set in a specified interval through scores, where the members of the ordered set are arranged in ascending (small to large) Order
Use: System.out.println(template.opsForZSet().rangeByScore("zset1",0,5));
Results: [zset-2, zset-1, zset-3]
  • Set<TypedTuple> rangeByScoreWithScores(K key, double min, double max);
    Returns the member objects in the specified interval of an ordered set through scores, in which the members of the ordered set are arranged in ascending (small to large) Order
Use: 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());
        }
//Result: value:zset-2score:1.2
value:zset-1score:2.2
value:zset-3score:2.3
  • Set rangeByScore(K key, double min, double max, long offset, long count);
    Returns the members in the specified interval of the ordered set through the score, and within the index range, the members of the ordered set are arranged in ascending order (from the smallest to the largest) by the score value
Use: System.out.println(template.opsForZSet().rangeByScore("zset1",0,5));
    System.out.println(template.opsForZSet().rangeByScore("zset1",0,5,1,2));
//Result:[zset-2, zset-1, zset-3]
[zset-1, zset-3]
  • Set<TypedTuple> rangeByScoreWithScores(K key, double min, double max, long offset, long count);
    Returns the member objects within the specified interval of the ordered set through scores, and within the index range, the members of the ordered set are arranged in ascending order (from the smallest to the largest) according to the scores
Use: 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());
        }
//Result: value:zset-1score:2.2
value:zset-3score:2.3
  • Set reverseRange(K key, long start, long end);
    The members in the specified interval are synthesized by returning the ordered set from the index interval, and the members of the ordered set are arranged in descending order (from large to small) according to the fractional value
Use: System.out.println(template.opsForZSet().reverseRange("zset1",0,-1));
Results: [zset-5, zset-4, zset-3, zset-1, zset-2]
  • Set<TypedTuple> reverseRangeWithScores(K key, long start, long end);
    Returns the ordered set through the index interval to synthesize the member objects in the specified interval, in which the members of the ordered set are arranged in descending order (from large to small) according to the fractional value
Use: 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());
        }
//Result: 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
  • Set reverseRangeByScore(K key, double min, double max);
    Use: as with the rangeByScore call method, the members of an ordered set are arranged in descending (large to small) order of fractional values

  • Set<TypedTuple> reverseRangeByScoreWithScores(K key, double min, double max);
    Use: as with the rangeByScoreWithScores call method, the members of an ordered set are arranged in descending (large to small) order of fractional values

  • Set reverseRangeByScore(K key, double min, double max, long offset, long count);
    Use: as with the rangeByScore call method, the members of an ordered set are arranged in descending (large to small) order of fractional values

  • Set<TypedTuple> reverseRangeByScoreWithScores(K key, double min, double max, long offset, long count);
    Use: as with the rangeByScoreWithScores call method, the members of an ordered set are arranged in descending (large to small) order of fractional values

  • Long count(K key, double min, double max);
    Returns the number of members in a specified interval of an ordered set through scores

Use: System.out.println(template.opsForZSet().rangeByScore("zset1",0,5));
        System.out.println(template.opsForZSet().count("zset1",0,5));
//Result:[zset-2, zset-1, zset-3]
3
  • Long size(K key);
    To get the number of members of an ordered set, the internal call is the zCard method
Use: System.out.println(template.opsForZSet().size("zset1"));
Results: 6
  • Long zCard(K key);
    Get the number of members of an ordered collection
Use: System.out.println(template.opsForZSet().zCard("zset1"));
Results: 6
  • Double score(K key, Object o);
    Gets the score value of the specified member
Use: System.out.println(template.opsForZSet().score("zset1","zset-1");
Results: 2.2
  • Long removeRange(K key, long start, long end);
    Removes the members of the specified index location, where the members of the ordered set are arranged in ascending (small to large) order of fractional values
Use: 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));
//Result:[zset-1, zset-2, zset-3, zset-4]
2
[zset-1, zset-4]
  • Long removeRangeByScore(K key, double min, double max);
    Remove members based on the specified score value range
Use://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));
//Result:[zset-1, zset-2, zset-3,zset-4]
1
[zset-1, zset-2, zset-4]
  • Long unionAndStore(K key, K otherKey, K destKey);
    Calculate the union of a given ordered set and store it in a new destKey. If the key is the same, the score value will be added
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().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());
        }
//Result: 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
  • Long unionAndStore(K key, Collection otherKeys, K destKey);
    Computes the union of a given ordered set and stores it in a new destKey
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().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().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());
        }
//Result: 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
  • Long intersectAndStore(K key, K otherKey, K destKey);
    Calculate the intersection of one or more ordered sets and store the result set in the new ordered set key
Use: 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());
        }
//Result: value:zset-1score:2.0
value:zset-2score:4.0
value:zset-3score:6.0
value:zset-4score:12.0
  • Long intersectAndStore(K key, Collection otherKeys, K destKey);
    Calculate the intersection of one or more ordered sets and store the result set in the new ordered set key
Use: 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());
        }
//Result: value:zset-1score:3.0
value:zset-2score:6.0
value:zset-3score:9.0
value:zset-4score:18.0
  • Cursor<TypedTuple<V>> scan(K key, ScanOptions options);
    Traversing zset
Use: 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());
        }
//Result: zset-1:1.0
zset-2:2.0
zset-3:3.0
zset-4:6.0

Note: TimeUnit is a class under the java.util.concurrent package, which represents the time period of given unit granularity
Commonly used particle size
TimeUnit.DAYS / / days
TimeUnit.HOURS / / hours
TimeUnit.MINUTES / / minutes
TimeUnit.SECONDS / / second
TimeUnit.MILLISECONDS / / MS

By dreamer rzc
Link: https://www.jianshu.com/p/7bf5dc61ca06
Source: Jianshu
The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.

685 original articles published, praised 1932, 250000 visitors+
His message board follow

Tags: Redis Java

Posted on Wed, 05 Feb 2020 01:08:09 -0500 by tieded