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在使用連接是沒(méi)用到pipeline,一條命令直接傳給Redis,Redis返回結(jié)果。而executePipelined實(shí)際上一條或多條命令,但是共用一個(gè)連接。
? ? /**
? ? ?* 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) { //開(kāi)啟管道
? ? ? ? ? ? ? ? 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é)果,和鍵的順序是一一對(duì)應(yīng)的,如果沒(méi)查到,會(huì)返回null值。
Java對(duì)Redis的批量操作RedisTemplate
1、背景
需求:一次性獲取redis緩存中多個(gè)key的value
潛在隱患:循環(huán)key,獲取value,可能會(huì)造成連接池的連接數(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的操作
- 針對(duì)數(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、說(shuō)明
本文簡(jiǎn)單的舉了關(guān)于RedisTemplate的兩個(gè)例子,但大家千萬(wàn)別以為只是批量取值的時(shí)候會(huì)用到,PipeLine其實(shí)是用來(lái)批量發(fā)送命令操作Redis。后來(lái)用Jedis也進(jìn)行了實(shí)現(xiàn),見(jiàn)下會(huì)分解。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Swagger2不被SpringSecurity框架攔截的配置及說(shuō)明
這篇文章主要介紹了Swagger2不被SpringSecurity框架攔截的配置及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Spring實(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-01
聊聊RabbitMQ發(fā)布確認(rèn)高級(jí)問(wèn)題
這篇文章主要介紹了RabbitMQ發(fā)布確認(rèn)高級(jí)問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
SpringBoot帶你實(shí)現(xiàn)一個(gè)點(diǎn)餐小程序
有個(gè)小伙伴臨時(shí)找到我,要開(kāi)發(fā)一個(gè)點(diǎn)餐的系統(tǒng),時(shí)間比較著急,給了2天的時(shí)間。馬馬虎虎的搞出來(lái)了,頭發(fā)掉了一撮!下面介紹下本系統(tǒng),感興趣的小伙伴,可以參考開(kāi)發(fā)下2022-07-07
mybatis如何實(shí)現(xiàn)in傳入數(shù)組查詢
這篇文章主要介紹了mybatis如何實(shí)現(xiàn)in傳入數(shù)組查詢方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教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-08
Springboot通過(guò)lucene實(shí)現(xiàn)全文檢索詳解流程
Lucene是一個(gè)基于Java的全文信息檢索工具包,它不是一個(gè)完整的搜索應(yīng)用程序,而是為你的應(yīng)用程序提供索引和搜索功能。Lucene 目前是 Apache Jakarta 家族中的一個(gè)開(kāi)源項(xiàng)目,也是目前最為流行的基于 Java 開(kāi)源全文檢索工具包2022-06-06

