Spring RedisTemplate 批量獲取值的2種方式小結(jié)
Spring RedisTemplate 批量獲取值
1、利用mGet
List<String> keys = new ArrayList<>(); //初始keys List<YourObject> list = this.redisTemplate.opsForValue().multiGet(keys);
2、利用PipeLine
List<YourObject> list = this.redisTemplate.executePipelined(new RedisCallback<YourObject>() { ? ? @Override ? ? public YourObject doInRedis(RedisConnection connection) throws DataAccessException { ? ? ? ? StringRedisConnection conn = (StringRedisConnection)connection; ? ? ? ? for (String key : keys) { ? ? ? ? ? ? conn.get(key); ? ? ? ? } ? ? ? ? return null; ? ? } });
其實(shí)2者底層都是用到execute方法,multiGet在使用連接是沒用到pipeline,一條命令直接傳給Redis,Redis返回結(jié)果。而executePipelined實(shí)際上一條或多條命令,但是共用一個連接。
? ? /** ? ? ?* Executes the given action object within a connection that can be exposed or not. Additionally, the connection can ? ? ?* be pipelined. Note the results of the pipeline are discarded (making it suitable for write-only scenarios). ? ? ?* ? ? ?* @param <T> return type ? ? ?* @param action callback object to execute ? ? ?* @param exposeConnection whether to enforce exposure of the native Redis Connection to callback code ? ? ?* @param pipeline whether to pipeline or not the connection for the execution ? ? ?* @return object returned by the action ? ? ?*/ ? ? public <T> T execute(RedisCallback<T> action, boolean exposeConnection, boolean pipeline) { ? ? ? ? Assert.isTrue(initialized, "template not initialized; call afterPropertiesSet() before using it"); ? ? ? ? Assert.notNull(action, "Callback object must not be null"); ? ? ? ? ? RedisConnectionFactory factory = getConnectionFactory(); ? ? ? ? RedisConnection conn = null; ? ? ? ? try { ? ? ? ? ? ? ? if (enableTransactionSupport) { ? ? ? ? ? ? ? ? // only bind resources in case of potential transaction synchronization ? ? ? ? ? ? ? ? conn = RedisConnectionUtils.bindConnection(factory, enableTransactionSupport); ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? conn = RedisConnectionUtils.getConnection(factory); ? ? ? ? ? ? } ? ? ? ? ? ? ? boolean existingConnection = TransactionSynchronizationManager.hasResource(factory); ? ? ? ? ? ? ? RedisConnection connToUse = preProcessConnection(conn, existingConnection); ? ? ? ? ? ? ? boolean pipelineStatus = connToUse.isPipelined(); ? ? ? ? ? ? if (pipeline && !pipelineStatus) { //開啟管道 ? ? ? ? ? ? ? ? connToUse.openPipeline(); ? ? ? ? ? ? } ? ? ? ? ? ? ? RedisConnection connToExpose = (exposeConnection ? connToUse : createRedisConnectionProxy(connToUse)); ? ? ? ? ? ? T result = action.doInRedis(connToExpose); ? ? ? ? ? ? ? if (pipeline && !pipelineStatus) {// 關(guān)閉管道 ? ? ? ? ? ? ? ? connToUse.closePipeline(); ? ? ? ? ? ? } ? ? ? ? ? ? ? // TODO: any other connection processing? ? ? ? ? ? ? return postProcessResult(result, connToUse, existingConnection); ? ? ? ? } finally { ? ? ? ? ? ? ? if (!enableTransactionSupport) { ? ? ? ? ? ? ? ? RedisConnectionUtils.releaseConnection(conn, factory); ? ? ? ? ? ? } ? ? ? ? } ? ? }
還有一點(diǎn),就是查詢返回的結(jié)果,和鍵的順序是一一對應(yīng)的,如果沒查到,會返回null值。
Java對Redis的批量操作RedisTemplate
1、背景
需求:一次性獲取redis緩存中多個key的value
潛在隱患:循環(huán)key,獲取value,可能會造成連接池的連接數(shù)增多,連接的創(chuàng)建和摧毀,消耗性能
解決方法:根據(jù)項(xiàng)目中的緩存數(shù)據(jù)結(jié)構(gòu)的實(shí)際情況,數(shù)據(jù)結(jié)構(gòu)為string類型的,使用RedisTemplate的multiGet方法;數(shù)據(jù)結(jié)構(gòu)為hash,使用Pipeline(管道),組合命令,批量操作redis。
2、操作
RedisTemplate的multiGet的操作
- 針對數(shù)據(jù)結(jié)構(gòu)為String類型
- 示例代碼
List<String> keys = new ArrayList<>(); for (Book e : booklist) { ? ?String key = generateKey.getKey(e); ? ?keys.add(key); } List<Serializable> resultStr = template.opsForValue().multiGet(keys);
此方法還是比較好用,使用者注意封裝。
RedisTemplate的Pipeline使用
1)方式一 : 基礎(chǔ)方式
- 使用類:StringRedisTemplate
- 使用方法
public executePipelined(RedisCallback<?> action) {...}
- 示例代碼:批量獲取value
List<Object> redisResult = redisTemplate.executePipelined(new RedisCallback<String>() { ? ?@Override ? ? public String doInRedis(RedisConnection redisConnection) throws DataAccessException { ? ? ? ? ? for (BooK e : booklist) { ? ? ? ?StringRedisConnection stringRedisConnection =(StringRedisConnection)redisConnection; ? ? ? ? stringRedisConnection.get(e.getId()); ? ? ? ? } ? ? ? ?return null; ? ? } });
方法二 : 使用自定義序列化方法
- 使用類:RedisTemplate
- 使用方法
public List<Object> executePipelined(final RedisCallback<?> action, final RedisSerializer<?> resultSerializer) {...}
- 示例代碼:批量獲取hash數(shù)據(jù)結(jié)構(gòu)value
List<Object> redisResult = redisTemplate.executePipelined( ? new RedisCallback<String>() { ? ? // 自定義序列化 ? ? RedisSerializer keyS = redisTemplate.getKeySerializer(); ? ? @Override ? ? public String doInRedis(RedisConnection redisConnection) throws DataAccessException { ? ? ? ? for (BooK e : booklist) { ? ? ? ? ? ? ? redisConnection.hGet(keyS.serialize(e.getName()), keyS.serialize(e.getAuthor())); ? ? ? ? } ? ? ? ? return null; ? ? } ? }, redisTemplate.getValueSerializer()); // 自定義序列化
3、說明
本文簡單的舉了關(guān)于RedisTemplate的兩個例子,但大家千萬別以為只是批量取值的時候會用到,PipeLine其實(shí)是用來批量發(fā)送命令操作Redis。后來用Jedis也進(jìn)行了實(shí)現(xiàn),見下會分解。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Swagger2不被SpringSecurity框架攔截的配置及說明
這篇文章主要介紹了Swagger2不被SpringSecurity框架攔截的配置及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03Spring實(shí)戰(zhàn)之使用TransactionProxyFactoryBean實(shí)現(xiàn)聲明式事務(wù)操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之使用TransactionProxyFactoryBean實(shí)現(xiàn)聲明式事務(wù)操作,結(jié)合實(shí)例形式分析了spring使用TransactionProxyFactoryBean實(shí)現(xiàn)聲明式事務(wù)相關(guān)配置、接口設(shè)置與使用技巧,需要的朋友可以參考下2020-01-01SpringBoot帶你實(shí)現(xiàn)一個點(diǎn)餐小程序
有個小伙伴臨時找到我,要開發(fā)一個點(diǎn)餐的系統(tǒng),時間比較著急,給了2天的時間。馬馬虎虎的搞出來了,頭發(fā)掉了一撮!下面介紹下本系統(tǒng),感興趣的小伙伴,可以參考開發(fā)下2022-07-07mybatis如何實(shí)現(xiàn)in傳入數(shù)組查詢
這篇文章主要介紹了mybatis如何實(shí)現(xiàn)in傳入數(shù)組查詢方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10基于SpringBoot實(shí)現(xiàn)驗(yàn)證碼功能(兩種驗(yàn)證碼方式)
這篇文章主要介紹了基于SpringBoot實(shí)現(xiàn)驗(yàn)證碼功能,今天我們介紹的是兩種主流的驗(yàn)證碼,一種就是進(jìn)行計(jì)算的驗(yàn)證碼,另外一種就是不需要計(jì)算,直接輸入的驗(yàn)證碼,需要的朋友可以參考下2024-08-08Springboot通過lucene實(shí)現(xiàn)全文檢索詳解流程
Lucene是一個基于Java的全文信息檢索工具包,它不是一個完整的搜索應(yīng)用程序,而是為你的應(yīng)用程序提供索引和搜索功能。Lucene 目前是 Apache Jakarta 家族中的一個開源項(xiàng)目,也是目前最為流行的基于 Java 開源全文檢索工具包2022-06-06