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)
刪除變量中的鍵值對,可以傳入多個參數(shù),刪除多個鍵值對。
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))); //緩存取出數(shù)據(jù)排序 Collections.sort(menuList, Comparator.comparing(BaseEntity::getId)); return menuList; } //不存在 數(shù)據(jù)庫中查詢并存入緩存 返回 List<Menu> allMenu = getMapper().selectMenus(); if(CollectionUtils.isNotEmpty(allMenu)){ allMenu.forEach(m -> redisTemplate.opsForHash().put("menuList", m.getId().toString(), JSONObject.toJSONString(m))); } return allMenu; }
以上就是redistemplate下opsForHash操作示例的詳細內(nèi)容,更多關于redistemplate opsForHash操作的資料請關注腳本之家其它相關文章!
- redis redistemplate序列化對象配置方式
- 配置redis的序列化,注入RedisTemplate方式
- 解讀RedisTemplate的各種操作(set、hash、list、string)
- 使用redisTemplate的scan方式刪除批量key問題
- Redis Template使用詳解示例教程
- RedisTemplate批量操作工具類性能測試
- 解決redisTemplate向redis中插入String類型數(shù)據(jù)時出現(xiàn)亂碼問題
- 解決RedisTemplate存儲至緩存數(shù)據(jù)出現(xiàn)亂碼的情況
- reids自定義RedisTemplate以及亂碼問題解決
相關文章
在CenOS系統(tǒng)下安裝和配置Redis數(shù)據(jù)庫的教程
這篇文章主要介紹了在CenOS系統(tǒng)下安裝和配置Redis數(shù)據(jù)庫的教程,Redis是一個可基于內(nèi)存的高性能NoSQL數(shù)據(jù)庫,需要的朋友可以參考下2015-11-11redis性能優(yōu)化之生產(chǎn)中實際遇到的問題及排查總結
這篇文章主要介紹了redis性能優(yōu)化之生產(chǎn)中實際遇到的問題及排查總結,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12redis與memcached的區(qū)別_動力節(jié)點Java學院整理
Memcached是以LiveJurnal旗下Danga Interactive公司的Bard Fitzpatric為首開發(fā)的高性能分布式內(nèi)存緩存服務器。那么redis與memcached有什么區(qū)別呢?下面小編給大家介紹下redis與memcached的區(qū)別,感興趣的朋友參考下吧2017-08-08