Spring boot集成redis lettuce代码实例
spring boot框架中已经集成了redis,在1.x.x的版本时默认使用的jedis客户端,现在是2.x.x版本默认使用的lettuce客户端
引入依赖
<!-- spring boot redis 缓存引入 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
<!-- redis依赖commons-pool 这个依赖一定要添加 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
配置文件
#Redis 配置
#Redis服务器地址
spring.redis.host=127.0.0.1
#Redis服务器连接端口
spring.redis.port=6379
#Redis服务器连接密码(默认为空)
spring.redis.password=123456
#Redis数据库索引(默认为0)
spring.redis.database=0
##连接超时时间
spring.redis.timeout=60s
# 以下连接池已在SpringBoot2.0不推荐使用
##连接池最大连接数(使用负值表示没有限制)
#spring.redis.jedis.pool.max-active=10
##连接池最大阻塞等待时间(使用负值表示没有限制)
#spring.redis.jedis.pool.max-wait=-1ms
##连接池中的最大空闲连接
#spring.redis.jedis.pool.max-idle=8
##连接池中的最小空闲连接
#spring.redis.jedis.pool.min-idle=0
# Lettuce
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.lettuce.pool.max-wait=10000
# 连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=0
# 关闭超时时间
spring.redis.lettuce.shutdown-timeout=100
配置config
@Configuration
@AutoConfigureAfter(RedisConfig.class)
public class RedisConfig {
// @Bean
// public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
// RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// redisTemplate.setKeySerializer(new StringRedisSerializer());
// redisTemplate.setHashKeySerializer(new StringRedisSerializer());
// redisTemplate.setHashValueSerializer(new StringRedisSerializer());
// redisTemplate.setValueSerializer(new StringRedisSerializer());
// redisTemplate.setConnectionFactory(factory);
// return redisTemplate;
// }
@Bean
public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory factory) {
RedisTemplate<String, Serializable> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new StringRedisSerializer());
template.setValueSerializer(new StringRedisSerializer());
template.setConnectionFactory(factory);
return template;
}
@Bean
public HashOperations<String, String, String> hashOperations(RedisTemplate<String, String> redisTemplate) {
return redisTemplate.opsForHash();
}
@Bean
public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) {
return redisTemplate.opsForValue();
}
@Bean
public SetOperations<String, String> setOperations(RedisTemplate<String, String> redisTemplate) {
return redisTemplate.opsForSet();
}
@Bean
public ListOperations<String, String> listOperations(RedisTemplate<String, String> redisTemplate) {
return redisTemplate.opsForList();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
- Java使用Lettuce客户端在Redis在主从复制模式下命令执行的操作
- 关于Springboot2.x集成lettuce连接redis集群报超时异常Command timed out after 6 second(s)
- springboot2整合redis使用lettuce连接池的方法(解决lettuce连接池无效问题)
- Redis Java Lettuce驱动框架原理解析
- SpringBoot整合Lettuce redis过程解析
- SpringBoot 整合 Lettuce Redis的实现方法
- Springboot2.X集成redis集群(Lettuce)连接的方法
- redis 使用lettuce 启动内存泄漏错误的解决方案
相关文章
Java定时任务Timer、TimerTask与ScheduledThreadPoolExecutor详解
这篇文章主要介绍了Java定时任务Timer、TimerTask与ScheduledThreadPoolExecutor详解, 定时任务就是在指定时间执行程序,或周期性执行计划任务,Java中实现定时任务的方法有很多,本文从从JDK自带的一些方法来实现定时任务的需求,需要的朋友可以参考下2024-01-01
PowerJob的DispatchStrategy方法工作流程源码解读
这篇文章主要为大家介绍了PowerJob的DispatchStrategy方法工作流程源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2024-01-01
Skywalking改成适配阿里云等带Http Basic的Elasticsearch服务
这篇文章主要介绍了改造Skywalking支持阿里云等带Http Basic的Elasticsearch服务2022-02-02
Java concurrency之AtomicLongArray原子类_动力节点Java学院整理
这篇文章主要介绍了Java concurrency之AtomicLongArray原子类的相关知识,感兴趣的朋友参考下吧2017-06-06
java中PriorityBlockingQueue的入队知识点总结
在本篇文章里小编给大家整理一篇关于java中PriorityBlockingQueue的入队知识点总结内容,有需要的朋友们可以学习下。2021-01-01


最新评论