RedisTemplate Common Collection Instructions - opsForHash (4)

     The basic configuration has been introduced in the previous " RedisTemplate Common Collection Usage Instructions (1) ", now we directly introduce the use of the opsForHash() method:

     1、put(H key, HK hashKey, HV value)

 

   Added hashMap value.

   

redisTemplate.opsForHash().put("hashValue","map1","map1-1");
redisTemplate.opsForHash().put("hashValue","map2","map2-2");

 

     2、values(H key)

 

  Get the hashMap value in the specified variable.

 

List<Object> hashList = redisTemplate.opsForHash().values("hashValue");
System.out.println("Get the hashMap value in the variable through the values(H key) method: " + hashList);

 

    3、entries(H key)

 

  Get the key-value pair in the variable.

 

Map<Object,Object> map = redisTemplate.opsForHash().entries("hashValue");
System.out.println("Get the key-value pair in the variable through the entries(H key) method: " + map);

 

    4、get(H key, Object hashKey)

 

  Gets whether the specified map key in the variable has a value, and if the map key exists, the value is obtained, otherwise null is returned.

 

Object mapValue = redisTemplate.opsForHash().get("hashValue","map1");
System.out.println("Get the value of map key by get(H key, Object hashKey) method: " + mapValue);

 

    5、hasKey(H key, Object hashKey)

 

      Determines whether the specified map key exists in the variable.

 

boolean hashKeyBoolean = redisTemplate.opsForHash().hasKey("hashValue","map3");
System.out.println("Use the hasKey(H key, Object hashKey) method to determine whether there is a map key in the variable: " + hashKeyBoolean);

 

     6、keys(H key)

 

       Get the key in the variable.

 

Set<Object> keySet = redisTemplate.opsForHash().keys("hashValue");
System.out.println("Get the key in the variable through the keys(H key) method: " + keySet);

 

     7、size(H key)

 

        Get the length of the variable.

 

long hashLength = redisTemplate.opsForHash().size("hashValue");
System.out.println("Get the length of the variable through the size(H key) method: " + hashLength);

 

     8、increment(H key, HK hashKey, double delta)

 

       Make the key in the variable auto-increment the size of the double value.

 

double hashIncDouble = redisTemplate.opsForHash().increment("hashInc","map1",3);
System.out.println("By the increment(H key, HK hashKey, double delta) method, the key in the variable is self-increased by the size of the value: " + hashIncDouble);

 

     9、increment(H key, HK hashKey, long delta)

 

Make the key in the variable auto-increment the size of the long value.

 

long hashIncLong = redisTemplate.opsForHash().increment("hashInc","map2",6);
System.out.println("By the increment(H key, HK hashKey, long delta) method, the key in the variable is self-increasing with the size of the value: " + hashIncLong);

     10 、multiGet(H key, Collection<HK> hashKeys)

 

         Get the value in a variable as a collection.

 

List<Object> list = new ArrayList<Object>();
list.add("map1");
list.add("map2");
List mapValueList = redisTemplate.opsForHash().multiGet("hashValue",list);
System.out.println("Get the value in the variable as a collection through the multiGet(H key, Collection<HK> hashKeys) method: "+mapValueList);

 

      11、putAll(H key, Map<? extends HK,? extends HV> m)

 

   Add key-value pairs as a map collection.

 

Map newMap = new HashMap();
newMap.put("map3","map3-3");
newMap.put("map5","map5-5");
redisTemplate.opsForHash().putAll("hashValue",newMap);
map = redisTemplate.opsForHash().entries("hashValue");
System.out.println("Add key-value pairs in the form of map collection by putAll(H key, Map<? extends HK,? extends HV> m) method: " + map);

 

      12、putIfAbsent(H key, HK hashKey, HV value)

 

          If the variable value exists, a non-existing key-value pair can be added to the variable. If the variable does not exist, a new variable will be added, and the key-value pair will be added to the variable at the same time.

 

redisTemplate.opsForHash().putIfAbsent("hashValue","map6","map6-6");
map = redisTemplate.opsForHash().entries("hashValue");
System.out.println("Add a key-value pair that does not exist in the variable through the putIfAbsent(H key, HK hashKey, HV value) method: " + map);

       13、scan(H key, ScanOptions options)

            Match to obtain key-value pairs, ScanOptions.NONE is to obtain all key pairs, ScanOptions.scanOptions().match("map1").build()     matches to obtain the key- value pairs of key map1 , which cannot be fuzzy matching.

Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan("hashValue",ScanOptions.scanOptions().match("map1").build());
//Cursor<Map.Entry<Object,Object>> cursor = redisTemplate.opsForHash().scan("hashValue",ScanOptions.NONE);
while (cursor.hasNext()){
    Map.Entry<Object,Object> entry = cursor.next();
    System.out.println("Get matching key-value pair by scan(H key, ScanOptions options) method:" + entry.getKey() + "---->" + entry.getValue());
}

        14delete(H key,Object... hashKeys) 

   Delete key-value pairs in variables. You can pass in multiple parameters to delete multiple key-value pairs .

redisTemplate.opsForHash().delete("hashValue","map1","map2");
map = redisTemplate.opsForHash().entries("hashValue");
System.out.println("Delete the key-value pair in the variable through the delete(H key, Object... hashKeys) method: " + map);

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327019745&siteId=291194637