最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring RedisTemplate 批量獲取值的2種方式小結(jié)

 更新時間:2022年06月07日 10:05:55   作者:Leon-Zheng  
這篇文章主要介紹了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;
? ? }
});

其實2者底層都是用到execute方法,multiGet在使用連接是沒用到pipeline,一條命令直接傳給Redis,Redis返回結(jié)果。而executePipelined實際上一條或多條命令,但是共用一個連接。

? ? /**
? ? ?* 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);
? ? ? ? ? ? }
? ? ? ? }
? ? }

還有一點,就是查詢返回的結(jié)果,和鍵的順序是一一對應(yīng)的,如果沒查到,會返回null值。

Java對Redis的批量操作RedisTemplate

1、背景

需求:一次性獲取redis緩存中多個key的value

潛在隱患:循環(huán)key,獲取value,可能會造成連接池的連接數(shù)增多,連接的創(chuàng)建和摧毀,消耗性能

解決方法:根據(jù)項目中的緩存數(shù)據(jù)結(jié)構(gòu)的實際情況,數(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其實是用來批量發(fā)送命令操作Redis。后來用Jedis也進行了實現(xiàn),見下會分解。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring如何使用通知?Advice?管理事務(wù)

    Spring如何使用通知?Advice?管理事務(wù)

    Spring 默認采用聲明式事務(wù)管理(通過配置的方式) ,也可以實現(xiàn)編程式事務(wù)管理,這篇文章主要介紹了Spring使用通知Advice管理事務(wù),需要的朋友可以參考下
    2023-06-06
  • 詳解JavaWeb如何實現(xiàn)文件上傳和下載功能

    詳解JavaWeb如何實現(xiàn)文件上傳和下載功能

    這篇文章主要介紹了如何利用JavaWeb實現(xiàn)文件的上傳和下載功能,文中的示例代碼講解詳細,對我們的學(xué)習(xí)或工作有一定的幫助,感興趣的小伙伴可以學(xué)習(xí)一下
    2021-12-12
  • java實現(xiàn)文件重命名功能

    java實現(xiàn)文件重命名功能

    這篇文章主要介紹了java實現(xiàn)文件重命名功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • JWT概述以及Token刷新機制詳解

    JWT概述以及Token刷新機制詳解

    這篇文章主要介紹了JWT概述以及Token刷新機制,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • IDEA的Terminal無法執(zhí)行g(shù)it命令問題

    IDEA的Terminal無法執(zhí)行g(shù)it命令問題

    這篇文章主要介紹了IDEA的Terminal無法執(zhí)行g(shù)it命令問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Java如何實現(xiàn)壓縮文件與解壓縮zip文件

    Java如何實現(xiàn)壓縮文件與解壓縮zip文件

    這篇文章主要介紹了Java如何實現(xiàn)壓縮文件與解壓縮zip文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 比較Java數(shù)組和各種List的性能小結(jié)

    比較Java數(shù)組和各種List的性能小結(jié)

    這篇文章主要是分別對Java數(shù)組、ArrayList、LinkedList和Vector進行隨機訪問和迭代等操作,并比較這種集合的性能。有需要的可以參考借鑒。
    2016-08-08
  • 基于springboot微信公眾號開發(fā)(微信自動回復(fù))

    基于springboot微信公眾號開發(fā)(微信自動回復(fù))

    這篇文章主要介紹了基于springboot微信公眾號開發(fā)(微信自動回復(fù)),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • 深入淺析springsecurity入門登錄授權(quán)

    深入淺析springsecurity入門登錄授權(quán)

    SpringSecurity為我們提供了基于注解的權(quán)限控制方案,這也是我們項目中主要采用的方式,我們可以使用注解去指定訪問對應(yīng)的資源所需的權(quán)限,這篇文章主要介紹了springsecurity入門登錄授權(quán),需要的朋友可以參考下
    2024-05-05
  • SpringBoot連接MYSQL數(shù)據(jù)庫并使用JPA進行操作

    SpringBoot連接MYSQL數(shù)據(jù)庫并使用JPA進行操作

    今天給大家介紹一下如何SpringBoot中連接Mysql數(shù)據(jù)庫,并使用JPA進行數(shù)據(jù)庫的相關(guān)操作。
    2017-04-04

最新評論

拜城县| 晋城| 庆元县| 微博| 福建省| 略阳县| 新晃| 那曲县| 武隆县| 郑州市| 阜新| 汤原县| 巩留县| 兴和县| 崇左市| 新源县| 九台市| 兴国县| 江安县| 集贤县| 卫辉市| 瑞昌市| 高陵县| 唐河县| 昂仁县| 延长县| 旬阳县| 房产| 体育| 康乐县| 健康| 岗巴县| 蒙阴县| 诏安县| 元谋县| 沂源县| 建平县| 湟中县| 江陵县| 赤城县| 阆中市|