乐趣区

关于java:redistemplate下opsForHash的操作

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); 

15. 缓存菜单的操作
public List<Menu> selectMenus() throws Exception {

    Collection<String> menujsons = redisTemplate.opsForHash().entries("menuList").values();
    // 查看缓存是否存在菜单
    if (menujsons != null && menujsons.size() != 0) {List<Menu> menuList = new ArrayList<>();
        menujsons.forEach(a -> menuList.add(JSONObject.parseObject(a, Menu.class)));
        // 缓存取出数据排序
        Collections.sort(menuList, Comparator.comparing(BaseEntity::getId));
        return menuList;
    }
    // 不存在 数据库中查问并存入缓存 返回
    List<Menu> allMenu = getMapper().selectMenus();
    if(CollectionUtils.isNotEmpty(allMenu)){allMenu.forEach(m -> redisTemplate.opsForHash().put("menuList", m.getId().toString(), JSONObject.toJSONString(m)));
    }
    return allMenu;
}
退出移动版