Spring Cache
1.简介
- spring从3.1开始定义了org.springframework.cache.Cache和org.springframework.cache.CacheManager接口来统一不同的缓存技术并且支持使用JCache(JSR-107)注解简化我们的开发
- Cache接口为缓存的组件规范定义,包含缓存的各种操作集合;Cache接口下的spring提供了各种xxxCache的实现:如RedisCache,EhCacheCache,ConcurrentMapCache等
- 每次调用需要缓存功能的方法时,spring会检查指定参数的指定的目标方法是否已经被调用过,如果有就直接从缓存中获取。
- 使用spring缓存抽象时我们需要关注以下两个点:
- 1.确定方法需要被缓存以及他们的缓存策略
- 2.从缓存中读取之前缓存存储的数据
2.快速开始
- 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
- 配置
-
自动配置
:::info
CacheAutoConfiguration自动导入RedisCacheConfiguration -->RedisCacheManager(缓存管理器)
::: -
配置redis作为缓存
-
- 测试并且使用缓存
@Cacheable: Triggers cache population. 触发,将数据保存到缓存
@CacheEvict: Triggers cache eviction. 触发将数据从缓存删除
@CachePut: Updates the cache without interfering with the method execution. 不影响方法执行更新缓存
@Caching: Regroups multiple cache operations to be applied on a method. 组合以上多个操作
@CacheConfig: Shares some common cache-related settings at class-level. 再类级别共享缓存的多个配置
- 开启缓存功能 @EnableCaching
- 只需要注解就能使用缓存
- 简单使用springcache
1、每一个需要缓存的数据我们都要去指定要放到那个名字的缓存
2、@Cacheable({“category”})
代表当前方法的结果需要缓存,如果缓存中有,方法不需要被调用
如果缓存中没有,会调用方法,最后将方法的结果放入缓存
3、默认行为
1)、如果缓存中有,方法不用调用。
2)、key是默认自动生成:缓存的名字::SimpleKey [](自主生成的key的值category::SimpleKey [])
3)、缓存的value的值,默认是使用Java序列化机制,将序列化之后的数据缓存到redis
4)、默认缓存时间 -1;
- 自定义操作
_ 1)、指定生成的缓存的key: key属性指定,接受一个SpEL
2)、指定缓存的数据的过期时间: 配置文件中修改
3)、将数据保存为json格式:_
@EnableConfigurationProperties(CacheProperties.class)
@Configuration
@EnableCaching
public class MyCacheConfig {
/**
* 配置文件中的配置没有生效
* 应该将配置文件中的配置
* 1、原来的配置文件绑定的配置类是这个样子
* @ConfigurationProperties(prefix = "spring.cache")
* public class CacheProperties
* 2、要让他生效
* @EnableConfigurationProperties(CacheProperties.class)
* @return
*/
@Bean
RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
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());
}
if (redisProperties.getKeyPrefix() != null) {
config = config.prefixKeysWith(redisProperties.getKeyPrefix());
}
if (!redisProperties.isCacheNullValues()) {
config = config.disableCachingNullValues();
}
if (!redisProperties.isUseKeyPrefix()) {
config = config.disableKeyPrefix();
}
return config;
}
}
- 注解的使用
- @Cacheable:新增数据
- 1、每一个需要缓存的数据我们都要去指定要放到那个名字的缓存
- 2、@Cacheable({“category”})
- @Cacheable:新增数据
代表当前方法的结果需要缓存,如果缓存中有,方法不需要被调用
如果缓存中没有,会调用方法,最后将方法的结果放入缓存
- _3、默认行为_
1)、如果缓存中有,方法不用调用。
2)、key是默认自动生成:缓存的名字::SimpleKey [](自主生成的key的值category::SimpleKey [])
3)、缓存的value的值,默认是使用Java序列化机制,将序列化之后的数据缓存到redis
4)、默认缓存时间 -1;
自定义操作
1)、指定生成的缓存的key: key属性指定,接受一个SpEL
2)、指定缓存的数据的过期时间: 配置文件中修改
3)、将数据保存为json格式:
- @CacheEvict:删除缓存数据
@CacheEvict(value = "category" , allEntries = true)
- @Caching:批量删除
@Caching(evict = {
@CacheEvict(value = "category" , key = "'getLevel1Category'"),
@CacheEvict(value = "category" , key = "'getCatalogJson'")
})
1)、同时进行多种缓存操作:@Caching(evict = {})
2)、指定删除某个分区下的所有数据:@CacheEvict(value = “category” , allEntries = true)
3)、存储同一类型的数据,都可以指定成同一个分区。分区名默认就是缓存的前缀
4)、 @CachePut 双写模式支持,一个修改缓存的操作
5)、spring-cache 的原理与不足:
a)、读模式:
(1)、缓存穿透:查询一个null数据。解决:缓存空数据:spring.cache.redis.cache-null-values=true
(2)、缓存击穿:大量并发进来之后查询一个正好过期的数据。解决:加锁。 默认是无锁状态:@Cacheable(value = {“category”} , key = “#root.methodName” , sync = true)
默认添加的为本地锁。
(3)、缓存雪崩:大量的key同时过期。解决:加随机时间。加过期时间:spring.cache.redis.time-to-live=3600000
b)、写模式:(缓存与数据库一致)
(1)、加读写锁
(2)、引入Canal,感知到数据库的更新去更新数据
(3)、读多写多的直接去查询数据库
6)、原理:
CacheManager(RedisCacheManager) --> Cache(RedisCache) --> Cache负责缓存的读写
- 总结:
常规数据(读多写少,即时性、一致性要求不高的数据),可以使用spring-cache。写模式只要缓存的数据有过期时间就足够了
特殊数据:特殊设计()