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

Redistemplate common collection instructions opsforhash (IV)

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 opsForHash() Use of methods :

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

newly added hashMap value .

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

2、values(H key)

Gets the value in the specified variable hashMap value .

List<Object> hashList = redisTemplate.opsForHash().values("hashValue");
System.out.println(" adopt values(H key) Method to get the in the variable hashMap value :" + hashList);

3、entries(H key)

Get the key value pair in the variable .

Map<Object,Object> map = redisTemplate.opsForHash().entries("hashValue");
System.out.println(" adopt entries(H key) Method to get the key value pair in the variable :" + map);

4、get(H key, Object hashKey)

Gets the specified value in the variable map Does the key have a value , If the map Key to get the value , No return null.

Object mapValue = redisTemplate.opsForHash().get("hashValue","map1");
System.out.println(" adopt get(H key, Object hashKey) Method to get map Value of key :" + mapValue);

5、hasKey(H key, Object hashKey)

Determine whether there is a specified... In the variable map key .

boolean hashKeyBoolean = redisTemplate.opsForHash().hasKey("hashValue","map3");
System.out.println(" adopt hasKey(H key, Object hashKey) Method to determine whether there is map key :" + hashKeyBoolean);

6、keys(H key)

​ Get the key in the variable .

Set<Object> keySet = redisTemplate.opsForHash().keys("hashValue");
System.out.println(" adopt keys(H key) Method to get the key in the variable :" + keySet);

7、size(H key)

​ Get the length of the variable .

long hashLength = redisTemplate.opsForHash().size("hashValue");
System.out.println(" adopt size(H key) Method to get the length of the variable :" + hashLength);

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

​ Make the key in the variable with double The size of the value increases by itself .

double hashIncDouble = redisTemplate.opsForHash().increment("hashInc","map1",3);
System.out.println(" adopt increment(H key, HK hashKey, double delta) Method to make the key in the variable self grow with the size of the value :" + hashIncDouble);

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

Make the key in the variable with long The size of the value increases by itself .

long hashIncLong = redisTemplate.opsForHash().increment("hashInc","map2",6);
System.out.println(" adopt increment(H key, HK hashKey, long delta) Method to make the key in the variable self grow with the size of the value :" + hashIncLong);

10、multiGet(H key, Collection hashKeys)

​ Get the value in the 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(" adopt multiGet(H key, Collection<HK> hashKeys) Method to get the value in the variable in the form of a collection :"+mapValueList);

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

With map Add key value pairs in the form of sets .

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(" adopt putAll(H key, Map<? extends HK,? extends HV> m) Methods to map Add key value pairs in the form of sets :" + map);

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

​ If the variable value exists , You can add non-existent key value pairs to variables , If the variable does not exist , Then add a new variable , At the same time, add the key value pair to the variable .

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

13、scan(H key, ScanOptions options)

​ Match to get key value pairs ,ScanOptions.NONE To get all key pairs ,ScanOptions.scanOptions().match(“map1”).build() Match to get the key map1 The key/value pair *,* No 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(" adopt scan(H key, ScanOptions options) Method to get a matching key value pair :" + entry.getKey() + "---->" + entry.getValue());
}

14、delete(H key, Object… hashKeys)

Delete key value pairs in variables , Multiple parameters can be passed in , Delete multiple key value pairs *.*

redisTemplate.opsForHash().delete("hashValue","map1","map2");
map = redisTemplate.opsForHash().entries("hashValue");
System.out.println(" adopt delete(H key, Object... hashKeys) Method deletes the remaining key value pairs in the variable :" + map);
原网站

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