1、put(H key,
HK hashKey,
HV value)
新增hashMap值。
- redisTemplate.opsForHash().put("hashValue","map1","map1-1");
- redisTemplate.opsForHash().put("hashValue","map2","map2-2");
2、values(H key)
獲取指定變量中的hashMap值。
- List<Object> hashList = redisTemplate.opsForHash().values("hashValue");
- System.out.println("通過values(H key)方法獲取變量中的hashMap值:" + hashList);
3、entries(H key)
獲取變量中的鍵值對。
- Map<Object,Object> map = redisTemplate.opsForHash().entries("hashValue");
- System.out.println("通過entries(H key)方法獲取變量中的鍵值對:" + map);
4、get(H key, Object hashKey)
獲取變量中的指定map鍵是否有值,如果存在該map鍵則獲取值,沒有則返回null。
- Object mapValue = redisTemplate.opsForHash().get("hashValue","map1");
- System.out.println("通過get(H key, Object hashKey)方法獲取map鍵的值:" + mapValue);
5、hasKey(H key,
Object hashKey)
判斷變量中是否有指定的map鍵。
- boolean hashKeyBoolean = redisTemplate.opsForHash().hasKey("hashValue","map3");
- System.out.println("通過hasKey(H key, Object hashKey)方法判斷變量中是否存在map鍵:" + hashKeyBoolean);
6、keys(H key)
獲取變量中的鍵。
- Set<Object> keySet = redisTemplate.opsForHash().keys("hashValue");
- System.out.println("通過keys(H key)方法獲取變量中的鍵:" + keySet);
7、size(H key)
獲取變量的長度。
- long hashLength = redisTemplate.opsForHash().size("hashValue");
- System.out.println("通過size(H key)方法獲取變量的長度:" + hashLength);
8、increment(H key, HK hashKey, double delta)
使變量中的鍵以double值的大小進行自增長。
- double hashIncDouble = redisTemplate.opsForHash().increment("hashInc","map1",3);
- System.out.println("通過increment(H key, HK hashKey, double delta)方法使變量中的鍵以值的大小進行自增長:" + hashIncDouble);
9、increment(H key, HK hashKey, long delta)
使變量中的鍵以long值的大小進行自增長。
- long hashIncLong = redisTemplate.opsForHash().increment("hashInc","map2",6);
- System.out.println("通過increment(H key, HK hashKey, long delta)方法使變量中的鍵以值的大小進行自增長:" + hashIncLong);
10、multiGet(H key,
Collection<HK> hashKeys)
以集合的方式獲取變量中的值。
- List<Object> list = new ArrayList<Object>();
- list.add("map1");
- list.add("map2");
- List mapValueList = redisTemplate.opsForHash().multiGet("hashValue",list);
- System.out.println("通過multiGet(H key, Collection<HK> hashKeys)方法以集合的方式獲取變量中的值:"+mapValueList);
11、putAll(H key, Map<? extends HK,? extends HV> m)
以map集合的形式添加鍵值對。
- 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("通過putAll(H key, Map<? extends HK,? extends HV> m)方法以map集合的形式添加鍵值對:" + map);
12、putIfAbsent(H key, HK hashKey, HV value)
如果變量值存在,在變量中可以添加不存在的的鍵值對,如果變量不存在,則新增一個變量,同時將鍵值對添加到該變量。
- redisTemplate.opsForHash().putIfAbsent("hashValue","map6","map6-6");
- map = redisTemplate.opsForHash().entries("hashValue");
- System.out.println("通過putIfAbsent(H key, HK hashKey, HV value)方法添加不存在於變量中的鍵值對:" + map);
13、scan(H key, ScanOptions options)
匹配獲取鍵值對,ScanOptions.NONE為獲取全部鍵對,ScanOptions.scanOptions().match("map1").build() 匹配獲取鍵位map1的鍵值對,不能模糊匹配。
- 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("通過scan(H key, ScanOptions options)方法獲取匹配鍵值對:" + entry.getKey() + "---->" + entry.getValue());
- }
14、delete(H key, Object... hashKeys)
刪除變量中的鍵值對,可以傳入多個參數,刪除多個鍵值對。
- redisTemplate.opsForHash().delete("hashValue","map1","map2");
- map = redisTemplate.opsForHash().entries("hashValue");
- System.out.println("通過delete(H key, Object... hashKeys)方法刪除變量中的鍵值對后剩余的:" + map);
本文轉載:https://blog.csdn.net/qq_39071667/article/details/88867639