Java Redis Template批量查詢指定鍵值對(duì)的實(shí)現(xiàn)
一.Redis使用pipeline批量查詢所有鍵值對(duì)
一次性獲取所有鍵值對(duì)的方式:
private RedisTemplate redisTemplate; @SuppressWarnings({ "rawtypes", "unchecked" }) ?? ?public List executePipelined(Collection<String> keySet) { ?? ??? ?return redisTemplate.executePipelined(new SessionCallback<Object>() { ?? ??? ??? ?@Override ?? ??? ??? ?public <K, V> Object execute(RedisOperations<K, V> operations) throws DataAccessException { ?? ??? ??? ??? ?HashOperations hashOperations = operations.opsForHash(); ?? ??? ??? ??? ?for (String key : keySet) { ?? ??? ??? ??? ??? ?hashOperations.entries(key); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?return null; ?? ??? ??? ?} ?? ??? ?}); ?? ?}
說(shuō)明: 上面的方法,可以將多個(gè)Redis 哈希表一次性取出,只有一次IO的時(shí)間。但也有個(gè)缺點(diǎn),當(dāng)哈希表中有個(gè)鍵值對(duì)中的內(nèi)容特別長(zhǎng)的時(shí)候,效率明顯下降。如果我們根本不需要這個(gè)鍵值對(duì),但每次都要將它取出,會(huì)大大浪費(fèi)性能,解決方案就是第二種方式。
二.批量獲取指定的鍵值對(duì)列表
/** ?? ? * 獲取批量keys對(duì)應(yīng)的列表中,指定的hash鍵值對(duì)列表 ?? ? * @param keys redis 鍵 ?? ? * @param hashKeys 哈希表鍵的集合(你需要獲取的那些鍵) ?? ? * @return ?? ? */ ?? ?@SuppressWarnings("unchecked") ?? ?public List<Map<String, String>> getSelectiveHashsList(List<String> keys, List<String> hashKeys) { ?? ??? ?List<Map<String, String>> hashList = new ArrayList<Map<String, String>>(); ?? ??? ?List<List<String>> pipelinedList = redisTemplate.executePipelined(new RedisCallback<Object>() { ?? ??? ??? ?@Override ?? ??? ??? ?public Object doInRedis(RedisConnection connection) throws DataAccessException { ?? ??? ??? ??? ?StringRedisConnection stringRedisConnection = (StringRedisConnection) connection; ?? ??? ??? ??? ?for (String key : keys) { ?? ??? ??? ??? ??? ?stringRedisConnection.hMGet(key, hashKeys.toArray(new String[hashKeys.size()])); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?return null; ?? ??? ??? ?} ?? ??? ?}); ?? ??? ?for (List<String> hashValueList : pipelinedList) { ?? ??? ??? ?Map<String, String> map = new LinkedHashMap<String, String>(); ?? ??? ??? ?for (int i = 0; i < hashValueList.size(); i++) { ?? ??? ??? ??? ?map.put(hashKeys.get(i), hashValueList.get(i)); ?? ??? ??? ?} ?? ??? ??? ?hashList.add(map); ?? ??? ?} ?? ??? ?return hashList; ?? ?}
使用示例:
可以批量取出你想要的人物屬性:
調(diào)用上述方法示例:
"tom","jack"是你想要操作的表;"name","age"是你想要獲取的屬性,想要幾個(gè)屬性,寫幾個(gè),提升請(qǐng)求速度。
getSelectiveHashsList(Arrays.asList("tom","jack"),Arrays.asList("name","age"));
到此這篇關(guān)于Java Redis Template批量查詢指定鍵值對(duì)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java Redis Template批量查詢指定鍵值對(duì)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
分布式難題ElasticSearch解決大數(shù)據(jù)量檢索面試
這篇文章主要為大家介紹了分布式面試難題,ElasticSearch解決大數(shù)據(jù)量檢索的問(wèn)題分析回答,讓面試官無(wú)話可說(shuō),幫助大家實(shí)現(xiàn)面試開(kāi)薪自由2022-03-03Spring Boot中使用Activiti的方法教程(二)
工作流(Workflow),就是“業(yè)務(wù)過(guò)程的部分或整體在計(jì)算機(jī)應(yīng)用環(huán)境下的自動(dòng)化”,下面這篇文章主要給大家介紹了關(guān)于Spring Boot中使用Activiti的相關(guān)資料,需要的朋友可以參考下2018-08-08SpringBoot中application.properties與application.yml區(qū)別小結(jié)
本文主要介紹了SpringBoot中application.properties與application.yml區(qū)別小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-10-10Java使用poi導(dǎo)出ppt文件的實(shí)現(xiàn)代碼
Apache POI 是用Java編寫的免費(fèi)開(kāi)源的跨平臺(tái)的 Java API,Apache POI提供API給Java對(duì)Microsoft Office格式檔案讀和寫的功能。本文給大家介紹Java使用poi導(dǎo)出ppt文件的實(shí)現(xiàn)代碼,需要的朋友參考下吧2021-06-06Java前端開(kāi)發(fā)之HttpServletRequest的使用
service方法中的request的類型是ServletRequest,而doGet/doPost方法的request的類型是HttpServletRequest,HttpServletRequest是ServletRequest的子接口,功能和方法更加強(qiáng)大2023-01-01Mybatisplus集成springboot完成分頁(yè)查詢功能(示例代碼)
今天小編給大家分享Mybatisplus集成springboot完成分頁(yè)查詢功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-11-11