Spring RedisTemplate 批量获取值的2种方式
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返回结果。而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) {// 关闭管道
connToUse.closePipeline();
}
// TODO: any other connection processing?
return postProcessResult(result, connToUse, existingConnection);
} finally {
if (!enableTransactionSupport) {
RedisConnectionUtils.releaseConnection(conn, factory);
}
}
}
还有一点,就是查询返回的结果,和键的顺序是一一对应的,如果没查到,会返回null值。
Spring RedisTemplate 批量获取值的2种方式相关推荐
- Spring Boot当中获取request的三种方式
本篇博客主要记录request相关知识,也是开发当中经常遇到的,感兴趣的跟小编一起学习吧! 目录 一.请求过程 二.获取request的三种方式 2.1.可以封装为静态方法 2.2.controlle ...
- Spring读取配置文件,获取bean的几种方式
Spring读取配置文件,获取bean的几种方式 方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemX ...
- Spring Boot 获取 Bean 的 3 种方式!还有谁不会?
作者 | chilx 来源 | https://blog.csdn.net/showchi/article/details/97005720 注意:调用者要被spring管理 方式一 注解@PostC ...
- python执行系统命令后获取返回值的几种方式集合
第一种情况 os.system('ps aux') 执行系统命令,没有返回值 第二种情况 result = os.popen('ps aux') res = result.read() for lin ...
- 【Spring杂烩】探讨Spring向容器注册Bean的三种方式
探讨Spring向容器注册Bean的三种方式 重点了解@Import实现的三种子方式 前提概要 Spring向容器注册Bean的三种方式 通过@ComponentScan.@Componet 通过@B ...
- Spring加载properties文件的两种方式
2019独角兽企业重金招聘Python工程师标准>>> 在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取pro ...
- 用spring管理和实例化对象的四种方式
用spring管理和实例化对象的四种方式 方式一:无参构造函数(最常用) 对象是spring创建,创建的对象还要交给spring容器管理 bean:豆子 id:在spring容器中Hello对象的名, ...
- ASP.NET页面之间传递值的几种方式
页面传值是学习asp.net初期都会面临的一个问题,总的来说有页面传值.存储对象传值.ajax.类.model.表单等.但是一般来说,常用的较简单有QueryString,Session,Cookie ...
- SpringBoot静态获取 bean的三种方式,你学会了吗?
欢迎关注方志朋的博客,回复"666"获面试宝典 来源:blog.csdn.net/showchi/article/details/97005720 注意:调用者要被spring管理 ...
最新文章
- html和css有序列表,HTMLCSS基础学习笔记14—有序列表及列表嵌套
- python numpy数组和one-hot编码相互转换
- 声明式编程与函数式编程_实用程序类与函数式编程无关
- 设计原则:不要为了复用而使用继承
- js中如何在不影响既有事件监听的前提下新增监听器
- MySQL Mathematical Functions(数学方法)
- ldflags android,Android 开发手记一 NDK编程实例
- 一文看尽 TensorFlow“奋斗史”!| CSDN 博文精选
- 巩固知识体系!mysql变量类型
- Qt_QPushButton 原生按钮长按
- c语言文字闪烁表白,C语言表白程序1颜色变化的心
- token验证失败 java_“Token校验失败,请检查确认”解决方法
- 数据库学习之多种数据库横向对比
- 基于fpga的FlexRay总线设计
- 微软服务器为何时间总是慢,登录微软账户特别慢的原因
- Android 11.0 进入recovery模式(等待用户选择recovery模式界面)进入自动恢复出厂设置模式
- android应用程序永久获取root权限方法,怎么使Android应用程序获得root权限
- 程序咖:体验沉浸式学习,评测中提升你的职业技能
- 互联网公司招聘--奇虎360--研发工程师--2016年笔试题
- 以太坊黄皮书(7~)
热门文章
- 让php来搞定一切!,ubuntu安装和配置php5
- 宝塔linux修改默认编码,宝塔linux面板防护CC设置(示例代码)
- mysql上机实验报告3_SQL入门随笔(上机实验报告)
- Leetcode每日一题:989.add-to-array-form-of-integer(数组形式的整数加法)
- c++中stack用法( 算法竞赛入门)
- pytorch tensor的数据类型
- 给 Sublime Text3 添加右键菜单
- python 3.7 replace函数的坑
- eclipse离线安装Activiti Designer插件
- [日常] 算法-单链表的创建-尾插法