Instructions for using RedisTemplate common collections - boundHashOps (7)

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

       First define a BoundHashOperations

BoundHashOperations<String, String, Object> boundHashOperations = redisTemplate.boundHashOps("li");

 1、put(HK key, HV value)

   Add an element to the specified key

boundHashOperations.put("ww","i");
boundHashOperations.put("w1","i1");
boundHashOperations.put("w2","i2");

 2、getKey()

  Get the value in the specified key

//Get the set binding key value
System.out.println("Get the set binding key value:" + boundHashOperations.getKey());

 3、values()

  Get the value in the map jdk requires 1.8 and above

//Get the value in the map
boundHashOperations.values().forEach(v -> System.out.println("Get the value in map" + v));

 4、entries()

     Get key-value pairs in map

//Get map key-value pair
boundHashOperations.entries().forEach((m,n) -> System.out.println("Get map key-value pair: " + m + "-" + n));

 5、get(Object member)

  Get the value in the map key

//Get the value of the map key
System.out.println("Get the value created by map:" + boundHashOperations.get("w1"));

6、keys()

  get key of map

//Get the key of the map
boundHashOperations.keys().forEach(v -> System.out.println("Get the keys of the map: " + v));

7、multiGet(Collection<HK> keys)

  Get map values ​​in batches based on map keys

//Get map values ​​in batches according to the map key
List list = new ArrayList<>(Arrays.asList("ww","w1"));
boundHashOperations.multiGet(list).forEach(v -> System.out.println("Batch get map value according to map key:" + v));

8、putAll(Map<? extends HK,? extends HV> m)

  Add key-value pairs in batches

//Add key-value pairs in batches
Map map = new HashMap<>();
map.put("m1","n1");
map.put("m2","n2");
boundHashOperations.putAll (map);
boundHashOperations.entries().forEach((m,n) -> System.out.println("Add key-value pairs in batches: " + m + "-" + n));

9、increment(HK key, long delta)

  The value of the auto-incrementing map key

// auto-increment the value of the map key
boundHashOperations.increment("c",1);
System.out.println("The value of the self-incrementing map key:" + boundHashOperations.get("c"));

10、putIfAbsent(HK key, HV value)

  Add a non-existing map key

//If the map key does not exist, it will be added, and if it exists, it will remain unchanged
boundHashOperations.putIfAbsent("m2","n2-1");
boundHashOperations.putIfAbsent("m3","n3");
boundHashOperations.entries().forEach((m,n) -> System.out.println("Add non-existing key-value pair: " + m + "-" + n));

 11、size()

   Get the map size corresponding to a specific key

//View the size of the map created by the binding
System.out.println("View the size of the map created by the binding:" + boundHashOperations.size());

12、scan(ScanOptions options)

  Scan all values ​​for a specific key

// Traverse the bound keys to get all the values
Cursor<Map.Entry<String, Object>> cursor = boundHashOperations.scan(ScanOptions.NONE);
while (cursor.hasNext()){
        Map.Entry<String, Object> entry = cursor.next();
         System.out.println("Loop through binding keys to get all values:" + entry.getKey() + "---" + entry.getValue());
 }

13、delete(Object... keys)

  Delete map values ​​in batches

long delSize = boundHashOperations.delete("m3","m2");
System.out.println("Number of deleted keys: " + delSize);
boundHashOperations.entries().forEach((m,n) -> System.out.println("Remaining map key-value pairs after deletion: " + m + "-" + n));

 

 

 

Guess you like

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