前言
❤Java学习路线个人总结-博客
❤欢迎点赞👍收藏⭐留言 📝分享给需要的小伙伴
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
添加配置
自动配置
- CacheAutoConfiguration 会导入 RedisCacheConfiguration;
- 会自动装配缓存管理器 RedisCacheManager;
- 启动类添加 @EnableCaching
- 方法添加Cacheable(value={“category”})
手动配置
@EnableConfigurationProperties(CacheProperties.class)
@Configuration
@EnableCaching
public class MyCacheConfig {
/**
* 配置文件的配置没有用上
* 1. 原来和配置文件绑定的配置类为:@ConfigurationProperties(prefix = "spring.cache")
* public class CacheProperties
* <p>
* 2. 要让他生效,要加上直接绑定类CacheProperties @EnableConfigurationProperties(CacheProperties.class)
*
* 1、第一种使用方式
* @Autowired
* CacheProperties cacheProperties
* 2、因为配置都在容器中了,可以直接作为参数传过来
* public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties)
*/
@Bean
public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
// config = config.entryTtl();
//JSON序列化数据 key value
config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
CacheProperties.Redis redisProperties = cacheProperties.getRedis();
//将配置文件中所有的配置都生效
if (redisProperties.getTimeToLive() != null) {
config = config.entryTtl(redisProperties.getTimeToLive());
}
// 指定key前缀
// 2.3之前使用prefixKeysWith 之后使用 prefixCacheNameWith or computePrefixWith
if (redisProperties.getKeyPrefix() != null) {
config = config.prefixCacheNameWith(redisProperties.getKeyPrefix());
}
//是否缓存null
if (!redisProperties.isCacheNullValues()) {
config = config.disableCachingNullValues();
}
//是否使用键前缀
if (!redisProperties.isUseKeyPrefix()) {
config = config.disableKeyPrefix();
}
return config;
}
}
application.yml
spring:
#cache配置
cache:
type: redis
redis:
#毫秒为单位,存活时间
time-to-live: 3600000
#指定redis KEY前缀 如果指定了前缀就用我们指定的前缀,如果没有就默认使用缓存的名字作为前缀,一般情况下不进行指定前缀还是用分区名称作为前缀可以组合或批量操作
key-prefix: CACHE_
#是否开启缓存前缀
use-key-prefix: true
#是否缓存空值,可以防止缓存穿透
cache-null-values: true
常用注解
- @Cacheable : 触发将数据保存到缓存的操作;
- @CacheEvict : 触发将数据从缓存删除的操作;
- @CachePut : 不影响方法执行更新缓存;
- @Cacheing: 组合以上多个操作;
- @CacheConfig: 在类级别共享缓存的相同配置;
缓存Key指定
属性名称 | 描述 | 示例 |
---|---|---|
methodName | 当前方法名 | #root.methodName |
method | 当前方法 | #root.method.name |
target | 当前被调用的对象 | #root.target |
targetClass | 当前被调用的对象的class | #root.targetClass |
args | 当前方法参数组成的数组 | #root.args[0] |
caches | 当前被调用的方法使用的Cache | #root.caches[0].name |
业务使用
添加缓存
value:缓存分区
key: 缓存Key
@Cacheable(value = {"category"}, key = "#root.method.name")
@Override
public Map<String,String> test() {
//查询数据
return null;
}
缓存失效
在修改数据的时候,修改完成会将缓存的数据清空
value:指定分区
key: 指定Key
@CacheEvict(value = {"category"}, key = "'test'")
public void updateTest(Test test) {
//更新数据
}
批量操作
@CacheEvict:组合多个操作;
//同时清除多个缓存操作
@Caching(evict = {
@CacheEvict(value = {"category"}, key = "'test'"),
@CacheEvict(value = {"category"}, key = "'test1'")
})
//指定删除某个分区一下的所有数据
@CacheEvict(value = {"category"}, allEntries = true)
修改数据
执行方法体 - 将结果缓存起来;
@CacheEvict(value = {"test"}, key = "#test.id")
public void updateTest(Test test) {
//更新数据
}