Spring boot集成redis lettuce代码实例

作者:穆晟铭 时间:2022-08-03 12:21:19 

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();
 }
}

来源:https://www.cnblogs.com/achengmu/p/11345220.html

标签:Spring,boot,redis,lettuce
0
投稿

猜你喜欢

  • JVM类加载机制原理及用法解析

    2021-08-17 00:22:29
  • java emoji表情存储的解决方法

    2023-07-10 20:19:06
  • IDEA社区版下载安装流程详解(小白篇)

    2021-11-13 12:37:18
  • 详解Spring Boot Profiles 配置和使用

    2021-10-05 22:54:57
  • spring依赖注入知识点分享

    2023-11-26 15:32:32
  • Spring Boot静态资源路径的配置与修改详解

    2022-09-15 19:27:08
  • Java将对象保存到文件中/从文件中读取对象的方法

    2022-06-18 21:26:42
  • 关于Java中的IO流总结(推荐)

    2023-08-23 18:13:56
  • C# 特殊的string类型详解

    2022-02-10 14:11:59
  • MyBatis注解CRUD与执行流程深入探究

    2023-07-03 06:19:44
  • Springboot项目全局异常统一处理案例代码

    2021-08-26 10:51:19
  • java 多态性详解及常见面试题

    2023-03-15 18:08:49
  • Java经典面试题最全汇总208道(六)

    2023-11-23 17:52:45
  • SpringBoot注解梳理(小结)

    2023-11-10 13:27:19
  • Android实现双曲线折线图

    2023-07-29 15:07:28
  • Hadoop组件简介

    2023-08-20 14:07:00
  • Spring声明式事务和@Aspect的拦截顺序问题的解决

    2023-07-18 12:10:46
  • Java日常练习题,每天进步一点点(61)

    2021-07-17 06:56:13
  • intellij idea使用git stash暂存一次提交的操作

    2023-03-08 08:30:11
  • Spring初始化与销毁顺序案例演示详解

    2021-06-08 14:33:10
  • asp之家 软件编程 m.aspxhome.com