1、利用mGet

  1. List<String> keys = new ArrayList<>();

  2. //初始keys

  3. List<YourObject> list = this.redisTemplate.opsForValue().multiGet(keys);

2、利用PipeLine

 
  1. List<YourObject> list = this.redisTemplate.executePipelined(new RedisCallback<YourObject>() {

  2. @Override

  3. public YourObject doInRedis(RedisConnection connection) throws DataAccessException {

  4. StringRedisConnection conn = (StringRedisConnection)connection;

  5. for (String key : keys) {

  6. conn.get(key);

  7. }

  8. return null;

  9. }

  10. });

其实2者底层都是用到execute方法,multiGet在使用连接是没用到pipeline,一条命令直接传给Redis,Redis返回结果。而executePipelined实际上一条或多条命令,但是共用一个连接。

 
  1. /**

  2. * Executes the given action object within a connection that can be exposed or not. Additionally, the connection can

  3. * be pipelined. Note the results of the pipeline are discarded (making it suitable for write-only scenarios).

  4. *

  5. * @param <T> return type

  6. * @param action callback object to execute

  7. * @param exposeConnection whether to enforce exposure of the native Redis Connection to callback code

  8. * @param pipeline whether to pipeline or not the connection for the execution

  9. * @return object returned by the action

  10. */

  11. public <T> T execute(RedisCallback<T> action, boolean exposeConnection, boolean pipeline) {

  12. Assert.isTrue(initialized, "template not initialized; call afterPropertiesSet() before using it");

  13. Assert.notNull(action, "Callback object must not be null");

  14. RedisConnectionFactory factory = getConnectionFactory();

  15. RedisConnection conn = null;

  16. try {

  17. if (enableTransactionSupport) {

  18. // only bind resources in case of potential transaction synchronization

  19. conn = RedisConnectionUtils.bindConnection(factory, enableTransactionSupport);

  20. } else {

  21. conn = RedisConnectionUtils.getConnection(factory);

  22. }

  23. boolean existingConnection = TransactionSynchronizationManager.hasResource(factory);

  24. RedisConnection connToUse = preProcessConnection(conn, existingConnection);

  25. boolean pipelineStatus = connToUse.isPipelined();

  26. if (pipeline && !pipelineStatus) { //开启管道

  27. connToUse.openPipeline();

  28. }

  29. RedisConnection connToExpose = (exposeConnection ? connToUse : createRedisConnectionProxy(connToUse));

  30. T result = action.doInRedis(connToExpose);

  31. if (pipeline && !pipelineStatus) {// 关闭管道

  32. connToUse.closePipeline();

  33. }

  34. // TODO: any other connection processing?

  35. return postProcessResult(result, connToUse, existingConnection);

  36. } finally {

  37. if (!enableTransactionSupport) {

  38. RedisConnectionUtils.releaseConnection(conn, factory);

  39. }

  40. }

  41. }

还有一点,就是查询返回的结果,和键的顺序是一一对应的,如果没查到,会返回null值。

Spring RedisTemplate 批量获取值的2种方式相关推荐

  1. Spring Boot当中获取request的三种方式

    本篇博客主要记录request相关知识,也是开发当中经常遇到的,感兴趣的跟小编一起学习吧! 目录 一.请求过程 二.获取request的三种方式 2.1.可以封装为静态方法 2.2.controlle ...

  2. Spring读取配置文件,获取bean的几种方式

    Spring读取配置文件,获取bean的几种方式 方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemX ...

  3. Spring Boot 获取 Bean 的 3 种方式!还有谁不会?

    作者 | chilx 来源 | https://blog.csdn.net/showchi/article/details/97005720 注意:调用者要被spring管理 方式一 注解@PostC ...

  4. python执行系统命令后获取返回值的几种方式集合

    第一种情况 os.system('ps aux') 执行系统命令,没有返回值 第二种情况 result = os.popen('ps aux') res = result.read() for lin ...

  5. 【Spring杂烩】探讨Spring向容器注册Bean的三种方式

    探讨Spring向容器注册Bean的三种方式 重点了解@Import实现的三种子方式 前提概要 Spring向容器注册Bean的三种方式 通过@ComponentScan.@Componet 通过@B ...

  6. Spring加载properties文件的两种方式

    2019独角兽企业重金招聘Python工程师标准>>> 在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取pro ...

  7. 用spring管理和实例化对象的四种方式

    用spring管理和实例化对象的四种方式 方式一:无参构造函数(最常用) 对象是spring创建,创建的对象还要交给spring容器管理 bean:豆子 id:在spring容器中Hello对象的名, ...

  8. ASP.NET页面之间传递值的几种方式

    页面传值是学习asp.net初期都会面临的一个问题,总的来说有页面传值.存储对象传值.ajax.类.model.表单等.但是一般来说,常用的较简单有QueryString,Session,Cookie ...

  9. SpringBoot静态获取 bean的三种方式,你学会了吗?

    欢迎关注方志朋的博客,回复"666"获面试宝典 来源:blog.csdn.net/showchi/article/details/97005720 注意:调用者要被spring管理 ...

最新文章

  1. html和css有序列表,HTMLCSS基础学习笔记14—有序列表及列表嵌套
  2. python numpy数组和one-hot编码相互转换
  3. 声明式编程与函数式编程_实用程序类与函数式编程无关
  4. 设计原则:不要为了复用而使用继承
  5. js中如何在不影响既有事件监听的前提下新增监听器
  6. MySQL Mathematical Functions(数学方法)
  7. ldflags android,Android 开发手记一 NDK编程实例
  8. 一文看尽 TensorFlow“奋斗史”!| CSDN 博文精选
  9. 巩固知识体系!mysql变量类型
  10. Qt_QPushButton 原生按钮长按
  11. c语言文字闪烁表白,C语言表白程序1颜色变化的心
  12. token验证失败 java_“Token校验失败,请检查确认”解决方法
  13. 数据库学习之多种数据库横向对比
  14. 基于fpga的FlexRay总线设计
  15. 微软服务器为何时间总是慢,登录微软账户特别慢的原因
  16. Android 11.0 进入recovery模式(等待用户选择recovery模式界面)进入自动恢复出厂设置模式
  17. android应用程序永久获取root权限方法,怎么使Android应用程序获得root权限
  18. 程序咖:体验沉浸式学习,评测中提升你的职业技能
  19. 互联网公司招聘--奇虎360--研发工程师--2016年笔试题
  20. 以太坊黄皮书(7~)

热门文章

  1. 让php来搞定一切!,ubuntu安装和配置php5
  2. 宝塔linux修改默认编码,宝塔linux面板防护CC设置(示例代码)
  3. mysql上机实验报告3_SQL入门随笔(上机实验报告)
  4. Leetcode每日一题:989.add-to-array-form-of-integer(数组形式的整数加法)
  5. c++中stack用法( 算法竞赛入门)
  6. pytorch tensor的数据类型
  7. 给 Sublime Text3 添加右键菜单
  8. python 3.7 replace函数的坑
  9. eclipse离线安装Activiti Designer插件
  10. [日常] 算法-单链表的创建-尾插法