在包含了 Spring - context-xxx.jar 的 Spring Boot 项目中,在启动类中添加@EnableCaching
注解,即可开启缓存功能。默认 Spring Cache 是不开启。
正常情况下使用 Redis 缓存工具时代码流程
使用 Spring Cache 后
在代码方法中编写对数据库的访问。只需要完成上面流程图中“从 mysql 取出”部分的
代码。剩余事情使用一个非常简单的注解即可。
pom.xml 中添加依赖
添加 Spring-boot-starter-web 会包含 spring-context。就可以使用 Spring Cache 了。
spring-boot-start-data-redis 添加后会有 Redis 配置环境,当前项目没有添加其他缓存工
具,所以 Spring Cache 会使用 Redis 作为缓存工具。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
启动类需要加 @EnableCaching
每个方法单独配置 key 前缀
每个方法单独配置 key 前缀适用于整个类中方法的前缀不统一的情况。
@Cacheable 表示要对方法返回值进行缓存。缓存时 key 的名称为 cacheName::key
cacheNames : 配置 key 的前缀
key:配置 key 的后缀。里面字符串要使用单引号。
全局配置类
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
@Configuration
public class SpringCacheConfig {
//
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
//缓存配置对象
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
redisCacheConfiguration = redisCacheConfiguration.entryTtl(Duration.ofMinutes(30L)) //设置缓存的默认超时时间:30分钟
// .disableCachingNullValues() //如果是空值,不缓存
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) //设置key序列化器
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); //设置value序列化器
return RedisCacheManager
.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(redisCacheConfiguration).build();
}
}
@Service
@CacheConfig(cacheNames = "com.bjsxt")
public class DemoServiceImpl implements DemoService {
@Override
// 固定字符串需要使用单引号
@Cacheable(key = "'demo'")
public String demo() {
System.out.println("demo方法被执行");
return "demo-result";
}
@Override
// Spring EL
// 字符串使用单引号
// #+方法参数名称:可以调用方法参数
@Cacheable(key = "'selectById'+#id")
public String selectById(Long id) {
System.out.println("执行了selectById:"+id);
return "selectById"+id;
}
@Override
@Cacheable(key = "'selectByUsername'+#id+':'+#username")
public People selectByUsername(Long id,String username) {
People people = new People();
people.setName(username);
people.setId(id);
return people;
}
@Override
@Cacheable(key = "'selectAll'")
public List<People> selectAll() {
List<People> list = new ArrayList<>();
list.add(new People(1l,"张三"));
list.add(new People(2l,"李四"));
return list;
}
@Override
@Cacheable(key = "'selectById2'+#id",condition = "#id!=5")
public People selectById2(Long id) {
System.out.println("执行selectById2");
return null;
}
@CacheEvict(key = "'demo'")
public void delete_demo(){
System.out.println("执行了删除");
}
@Override
@CachePut(key = "'selectById'+#id")
public String selectByIdUpdate(Long id) {
System.out.println("修改update");
return "selectByIdUpdate"+id;
}
}
condition 和 unless 属性
@Cacheable 中
condition 是普通条件。如果条件成立则进行缓存。此判断为进入到方法体之前的判
断,所以#result 不允许用在这里。如果在 condition 中出现#result 会导致条件恒不成立,不
进行缓存。
unless 是方法执行完成后的条件,当符合条件不被缓存。注意:#result 只能写在这
个属性中。多用在返回结果为 null 时不缓存效果。
删除缓存
使用@CacheEvict 进行删除
@CacheEvict(key = "'demo'")
@Override
public void delete() {
System.out.println("执行 delete");
}
修改缓存
每次都会执行方法,并把方法会返回重新新增到 redis 中(redis 同名 key 新增是修改)
// Cache 每次都走方法体,无论 redis 中是否存在指定 key.
// 恒执行 redis 的新增操作
@CachePut(key = "'update'")
@Override
public String update() {
System.out.println("执行修改");
return "update2";
}