SpringBoot整合Redis

文章目录

  • 简介
  • 依赖
  • 配置
  • 使用

简介

刚开始学习redis的时候,用的是原生的的jedis,每次都去new一个对象,然后来用,在与SpringBoot整合后,被Spring托管了,然后可以可以自动装配了,感觉挺爽的,官方提供了一个RedisTemplate,我们想要使用只需导入依赖并简单的配置就行了。

依赖

先说一下这里的SpringBoot版本
2.2.7.RELEASE


    org.springframework.boot
    spring-boot-starter-data-redis

点进去此依赖可以看到,底层用的是lettuce


  io.lettuce
  lettuce-core
  5.2.2.RELEASE
  compile

配置

配置中用的也都是lettuce

spring.redis.host=127.0.0.1
spring.redis.port=6379

# 下面是连接池的配置
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.lettuce.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=0

使用

// 注入RedisTemplate 
@Autowired
RedisTemplate redisTemplate;
// redisTemplate 操作不同的数据类型
// opsForValue 操作字符串,类似于String
// opsForList 操作list,类似于List
// 等等...
redisTemplate.opsForValue().set("k1","v1");
System.out.println(redisTemplate.opsForValue().get("k1"));

你可能感兴趣的:(Java框架,Redis)