【spring boot】spring boot 基于redis pipeline 管道,批量操作redis命令
spring boot 2.x
使用RedisTemplate 操作
===================================
1.pom.xml
<!--spring2.0集成redis所需common-pool2-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<!-- 使用redis的LUA脚本 需要序列化操作的jar-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
2.redisConfig 需要加入spring的自动配置
/**
* @author sxd
* @date 2019/5/27 16:13
*/
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig { @Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(factory);
RedisSerializer keySerializer = new StringRedisSerializer();
// RedisSerializer valueSerializer = new GenericJackson2JsonRedisSerializer();
//key采用字符串反序列化对象
redisTemplate.setKeySerializer(keySerializer);
//value也采用字符串反序列化对象
//原因:管道操作,是对redis命令的批量操作,各个命令返回结果可能类型不同
//可能是 Boolean类型 可能是String类型 可能是byte[]类型 因此统一将结果按照String处理
redisTemplate.setValueSerializer(keySerializer);
return redisTemplate;
} }
3.controller
@Autowired
RedisTemplate redisTemplate; /**
* redis 批量操作其中一种方式
* redis pipeline 管道技术
*/
@RequestMapping(value = "/redisPipeline" )
public void redisPipeline(){ // 1.executePipelined 重写 入参 RedisCallback 的doInRedis方法
List<Object> resultList = redisTemplate.executePipelined(new RedisCallback<Object>() { @Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
// 2.connection 打开管道
connection.openPipeline(); // 3.connection 给本次管道内添加 要一次性执行的多条命令 // 3.1 一个set操作
byte[] key1 = "mykey1".getBytes();
byte[] value1 = "字符串value".getBytes();
connection.set(key1,value1); // 3.2一个批量mset操作
Map<byte[],byte[]> tuple = new HashMap<>();
tuple.put("m_mykey1".getBytes(),"m_value1".getBytes());
tuple.put("m_mykey2".getBytes(),"m_value2".getBytes());
tuple.put("m_mykey3".getBytes(),"m_value3".getBytes());
connection.mSet(tuple); // 3.3一个get操作
connection.get("m_mykey2".getBytes()); // 4.关闭管道 不需要close 否则拿不到返回值
// connection.closePipeline(); // 这里一定要返回null,最终pipeline的执行结果,才会返回给最外层
return null;
}
}); // 5.最后对redis pipeline管道操作返回结果进行判断和业务补偿
for (Object str : resultList) {
System.out.println(String.valueOf(str));
} }
运行结果:
true
true
m_value2
redis中结果:

【spring boot】spring boot 基于redis pipeline 管道,批量操作redis命令的更多相关文章
- 使用pipeline管道执行redis命令
pipeline管道可以减少后端与redis的连接次数,从而实现了优化. 原理如下: 使用方法: 未使用pipeline前: strict_redis = get_redis_connection(' ...
- Python Redis pipeline操作和Redis乐观锁保持数据一致性
Redis是建立在TCP协议基础上的CS架构,客户端client对redis server采取请求响应的方式交互. redis 乐观锁:也可理解为版本号比较机制,主要是说在读取数据逇时候同时读取其版本 ...
- 等待 Redis 应答 Redis pipeline It's not just a matter of RTT
小结: 1.When pipelining is used, many commands are usually read with a single read() system call, and ...
- Cola Cloud 基于 Spring Boot, Spring Cloud 构建微服务架构企业级开发平台
Cola Cloud 基于 Spring Boot, Spring Cloud 构建微服务架构企业级开发平台: https://gitee.com/leecho/cola-cloud
- Spring Data JPA例子[基于Spring Boot、Mysql]
关于Spring Data Spring社区的一个顶级工程,主要用于简化数据(关系型&非关系型)访问,如果我们使用Spring Data来开发程序的话,那么可以省去很多低级别的数据访问操作,如 ...
- Spring boot 、mybatis、swagger、c3p0、redis 和mongodb 整合
文件路径: 添加依赖: <?xml version="1.0" encoding="UTF-8"?> <project ...
- Spring boot 、swagger、c3p0、mybatis和redis 整合
文件路径 添加依赖 <?xml version="1.0" encoding="UTF-8"?> <projec ...
- Spring Boot简化了基于Spring的应用开发
Spring Boot简化了基于Spring的应用开发,通过少量的代码就能创建一个独立的.产品级别的Spring应用. Spring Boot为Spring平台及第三方库提供开箱即用的设置,这样你就可 ...
- [权限管理系统(四)]-spring boot +spring security短信认证+redis整合
[权限管理系统]spring boot +spring security短信认证+redis整合 现在主流的登录方式主要有 3 种:账号密码登录.短信验证码登录和第三方授权登录,前面一节Sprin ...
随机推荐
- linux线程绑定cpu
函数介绍 #define __USE_GNU #include <sched.h> void CPU_ZERO(cpu_set_t *set); void CPU_SET(int cpu, ...
- Qt时间转换 当前时间
当前时间 qDebug() << QTime::currentTime().toString(Qt::ISODate); //"15:23:48" qDebug() & ...
- LNK1104 无法打开文件“xxx.lib”
尝试解决方法: 1.找到这个库,把这个库移动到特定的文件夹下,在属性中添加这个库: 具体来说:打开VS项目->项目属性->配置属性->C/C+±>附加包含目录->编辑-& ...
- nginx geoip_module 地域信息读取
1.安装geoip yum -y install nginx-module-geoip 2.安装完成后在cd /etc/nginx/modules/ 能看到安装的模块 # cd /etc/nginx/ ...
- day29 8_8 TCP上传文件socketserver的应用
一.文件上传 对于一些比较大的文件,当传输的数据大于内存时,显然,一次性将数据读取到内存中,在从内存传输到服务器显然时不可取的. 所以,在上传文件时,可以在with open打开文件,边读取文件边发送 ...
- zz目标检测
deep learning分类 目标检测-HyperNet-论文笔记 06-06 基础DL模型-Deformable Convolutional Networks-论文笔记 06-05 基础DL模型- ...
- spring框架的定时任务cronExpression表达式详解
附:cronExpression表达式解释: 0 0 12 * * ?---------------在每天中午12:00触发 0 15 10 ? * *---------------每天上午10:15 ...
- c++基础第一篇
前言:我是从c和c++对比的角度来讲解c++的基础知识. (1)c++格式如下: #include <iostream> //标准输入输出头文件 using namespace std; ...
- 显示隐藏文件.reg
显示隐藏文件.reg Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Microsoft\Windows\Curren ...
- [LeetCode] 901. Online Stock Span 股票价格跨度
Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of ...