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值的大小進(jìn)行自增長。
double hashIncDouble = redisTemplate.opsForHash().increment("hashInc","map1",3); System.out.println("通過increment(H key, HK hashKey, double delta)方法使變量中的鍵以值的大小進(jìn)行自增長:" + hashIncDouble);
9、increment(H key, HK hashKey, long delta)
使變量中的鍵以long值的大小進(jìn)行自增長。
long hashIncLong = redisTemplate.opsForHash().increment("hashInc","map2",6); System.out.println("通過increment(H key, HK hashKey, long delta)方法使變量中的鍵以值的大小進(jìn)行自增長:" + 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操作示例的詳細(xì)內(nèi)容,更多關(guān)于redistemplate opsForHash操作的資料請關(guān)注腳本之家其它相關(guān)文章!
- 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以及亂碼問題解決
相關(guān)文章
在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)中實(shí)際遇到的問題及排查總結(jié)
這篇文章主要介紹了redis性能優(yōu)化之生產(chǎn)中實(shí)際遇到的問題及排查總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12Redis過期監(jiān)聽機(jī)制,訂單超時自動取消方式
這篇文章主要介紹了Redis過期監(jiān)聽機(jī)制,訂單超時自動取消方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05Redis在計數(shù)器和人員記錄的事務(wù)操作應(yīng)用小結(jié)
Redis是一個高性能的鍵值存儲系統(tǒng),專于處理計數(shù)器和事務(wù)操作,它提供了INCR、DECR等命令來進(jìn)行原子遞增或遞減操作,并通過MULTI、EXEC等命令實(shí)現(xiàn)事務(wù)操作,此外,Redis的Pipeline功能可減少網(wǎng)絡(luò)往返次數(shù),提高性能2024-10-10redis與memcached的區(qū)別_動力節(jié)點(diǎn)Java學(xué)院整理
Memcached是以LiveJurnal旗下Danga Interactive公司的Bard Fitzpatric為首開發(fā)的高性能分布式內(nèi)存緩存服務(wù)器。那么redis與memcached有什么區(qū)別呢?下面小編給大家介紹下redis與memcached的區(qū)別,感興趣的朋友參考下吧2017-08-08Redis實(shí)現(xiàn)唯一計數(shù)的3種方法分享
這篇文章主要介紹了Redis實(shí)現(xiàn)唯一計數(shù)的3種方法分享,本文講解了基于SET、基于 bit、基于 HyperLogLog三種方法,需要的朋友可以參考下2015-03-03利用Redis如何實(shí)現(xiàn)自動補(bǔ)全功能
這篇文章主要給大家介紹了關(guān)于如何利用Redis如何實(shí)現(xiàn)自動補(bǔ)全功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Redis具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09