SpringBoot 整合 Redis
Redis 是一个高性能的 key-value 数据库,可以用来缓存数据、实现消息队列等等。Spring Boot 提供了非常方便的方式来整合 Redis。
本文将介绍如何在 Spring Boot 中整合 Redis,包括如何配置 Redis 连接、如何使用 RedisTemplate 进行数据操作、如何使用 SpringCache 抽象层实现缓存。
配置 Redis 连接
要在 Spring Boot 中使用 Redis,首先需要添加 Redis 相关的依赖。在 Maven 项目中,可以添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--连接池-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
添加依赖后,需要在配置文件中指定 Redis 连接信息。可以在
application.properties
或application.yml
文件中添加以下配置:
spring:
redis:
host: localhost
port: 6379
database: 0
lettuce:
pool:
max-active: 8 # 连接池最大连接数(使用负值表示没有限制) 默认 8
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
max-idle: 8 # 连接池中的最大空闲连接 默认 8
min-idle: 0 # 连接池中的最小空闲连接 默认 0
password: ok
上述配置中,指定了 Redis 的主机名、端口和密码。如果 Redis 没有设置密码,则可以将
spring.redis.password
留空。
配置RedisTemplate
RedisTemplate中需要设置序列化组件与SpringCache默认的序列化组件一致。
不一致的话,使用
@Cacheable
,@CachePut
,@CacheEvict
操作值的后,和用RedisTemplate操作值可能存在找不到key
和无法反序列化value
的问题
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
@Configuration
public class RedisConfig{
@Resource
private RedisTemplate redisTemplate;
@PostConstruct
public void init(){
RedisSerializer stringSerializer = redisTemplate.getStringSerializer();
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
}
}
使用 RedisTemplate 进行数据操作
在 Spring Boot 中,可以使用
RedisTemplate
来进行 Redis 操作。可以通过注入RedisTemplate
来使用它,例如:
@Autowired
private RedisTemplate<String, String> redisTemplate;
上述代码注入了一个
RedisTemplate
,可以使用它来进行 String 类型的操作。如果需要操作其他类型的数据,可以使用不同的泛型参数。例如,如果需要操作 Redis 中的对象,可以使用以下方式进行注入:
@Autowired
private RedisTemplate<String, Object> redisTemplate;
使用
RedisTemplate
进行数据操作非常方便。例如,可以使用以下代码向 Redis 中添加一个字符串:
redisTemplate.opsForValue().set("key", "value");
可以使用以下代码从 Redis 中获取一个字符串:
String value = redisTemplate.opsForValue().get("key");
除了
opsForValue()
方法,RedisTemplate
还提供了许多其他方法,可以进行各种数据操作,例如opsForHash()
、opsForList()
、opsForSet()
等等。具体使用方法可以参考 Spring Boot 官方文档。
使用 SpringCache 抽象层实现缓存
除了使用
RedisTemplate
进行 Redis 操作,Spring Boot 还提供了一个缓存抽象层,可以用来简化缓存的使用。可以通过注解的方式来使用 Spring Cache 抽象层。在启动类上加上@EnableCache
即可
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class RedisDemoApplication {
public static void main(String[] args) {
SpringApplication.run(PathMatchApplication.class, args);
}
}
SpringCache常用注解
@Cacheable
,@CachePut
,@CacheEvict
,@Caching
注解 | 作用 | 属性 | 意义 |
---|---|---|---|
@Cacheable | 声明方法的返回值可以被缓存,调用方法时,直接从缓存中获取结果,如果缓存有,则返回缓存数据,而不是执行方法体。 | value | 缓存名称,缺省时使用类名作为名称。 |
key | 缓存 Key 的表达式,用于指定缓存的 Key 值,缺省时使用方法的所有参数作为 Key 值。 | ||
condition | 缓存条件表达式,用于指定是否进行缓存,缺省时总是进行缓存。 | ||
unless | 排除缓存的条件表达式,用于指定是否排除某些缓存结果,缺省时总是缓存。 | ||
@CachePut | 更新缓存中的数据,它会使方法执行,并将结果存储到缓存中。 | value | 缓存名称,缺省时使用类名作为名称。 |
key | 缓存 Key 的表达式,用于指定缓存的 Key 值,缺省时使用方法的所有参数作为 Key 值。 | ||
condition | 缓存条件表达式,用于指定是否进行缓存,缺省时总是进行缓存。 | ||
unless | 排除缓存的条件表达式,用于指定是否排除某些缓存结果,缺省时总是缓存。 | ||
@CacheEvict | 从缓存中删除数据,它可以指定要删除的缓存数据、要删除的缓存 Key 值、以及删除前后是否执行方法体。 | value | 缓存名称,缺省时使用类名作为名称。 |
key | 缓存 Key 的表达式,用于指定缓存的 Key 值,缺省时使用方法的所有参数作为 Key 值。 | ||
condition | 缓存条件表达式,用于指定是否进行缓存,缺省时总是进行缓存。 | ||
allEntries | 是否删除所有缓存数据,缺省为 false 。 | ||
beforeInvocation | 是否在方法执行前删除缓存数据,缺省为 false 。 | ||
@Caching | 用于组合多个注解,以便在同一个方法上执行多个缓存操作。 | cacheable | @Cacheable 注解,用于声明方法的返回值可以被缓存。 |
put | @CachePut 注解,用于更新缓存中的数据。 | ||
evict | @CacheEvict 注解,用于删除缓存中的数据。 |
案例
使用
@CachePut
存值使用@Cacheable
和redisTemplate
取值
//存值
@CachePut(key = "#key",value = "myRedis")
public Map saveMap(String key,Map map){
return map;
}
//取值
@Cacheable(key = "#key",value = "myRedis")
public Map getMap(String key){
return null;
}
@Resource
private RedisTemplate redisTemplate;
//取值
public Map getMapByRedisTemplate(String value,String key){
//获取
return redisTemplate.opsForValue().get(value+"::"+key)
}
//获取所有的已${value}+"::"的值
public List<Map> getMapByRedisTemplate(String value){
Set<String> keys = redisTemplate.keys(value+"::*");
List<Map> list = new ArrayList();
keys.forEach(e->{
Map map = (Map) redisTemplate.opsForValue().get(e);
list.add(map);
});
return list
}