Java Redis Template批量查詢指定鍵值對的實現(xiàn)
一.Redis使用pipeline批量查詢所有鍵值對
一次性獲取所有鍵值對的方式:
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; ?? ??? ??? ?} ?? ??? ?}); ?? ?}
說明: 上面的方法,可以將多個Redis 哈希表一次性取出,只有一次IO的時間。但也有個缺點,當哈希表中有個鍵值對中的內容特別長的時候,效率明顯下降。如果我們根本不需要這個鍵值對,但每次都要將它取出,會大大浪費性能,解決方案就是第二種方式。
二.批量獲取指定的鍵值對列表
/** ?? ? * 獲取批量keys對應的列表中,指定的hash鍵值對列表 ?? ? * @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; ?? ?}
使用示例:
可以批量取出你想要的人物屬性:
調用上述方法示例:
"tom","jack"是你想要操作的表;"name","age"是你想要獲取的屬性,想要幾個屬性,寫幾個,提升請求速度。
getSelectiveHashsList(Arrays.asList("tom","jack"),Arrays.asList("name","age"));
到此這篇關于Java Redis Template批量查詢指定鍵值對的實現(xiàn)的文章就介紹到這了,更多相關Java Redis Template批量查詢指定鍵值對內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
分布式難題ElasticSearch解決大數(shù)據(jù)量檢索面試
這篇文章主要為大家介紹了分布式面試難題,ElasticSearch解決大數(shù)據(jù)量檢索的問題分析回答,讓面試官無話可說,幫助大家實現(xiàn)面試開薪自由2022-03-03Spring Boot中使用Activiti的方法教程(二)
工作流(Workflow),就是“業(yè)務過程的部分或整體在計算機應用環(huán)境下的自動化”,下面這篇文章主要給大家介紹了關于Spring Boot中使用Activiti的相關資料,需要的朋友可以參考下2018-08-08SpringBoot中application.properties與application.yml區(qū)別小結
本文主要介紹了SpringBoot中application.properties與application.yml區(qū)別小結,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-10-10Java前端開發(fā)之HttpServletRequest的使用
service方法中的request的類型是ServletRequest,而doGet/doPost方法的request的類型是HttpServletRequest,HttpServletRequest是ServletRequest的子接口,功能和方法更加強大2023-01-01Mybatisplus集成springboot完成分頁查詢功能(示例代碼)
今天小編給大家分享Mybatisplus集成springboot完成分頁查詢功能,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2023-11-11