(分区)
常用注解
- @Cacheable :触发将数据保存到缓存的操作;
- @CacheEvict : 触发将数据从缓存删除的操作;(清除模式)
- @CachePut :不影响方法执行更新缓存;(双写模式:在修改后返回要存在缓存的数据,再次更新缓存)
- @Cacheing:组合以上多个操作;(清除多个缓存)
- @CacheConfig:在类级别共享缓存的相同配置;
案例
缓存一般适用于 热点数据和业务代码写的比较水的程序员(四级类目用递归和for循环实现每次都进行重复查询)
//缓存redis key值是以方法名加两个参数形成
@Cacheable(value = {"category"}, key = "#root.method.name+#type+#status", sync = true)
public String selectCatalogue(int type,int status)
//删除缓存 在增删改的方法上面需要都进行加注解清除 适用于一些不经常变化的数据
@CacheEvict(value ={"category"},key ="'selectCatalogue'+#catalogue.type+#catalogue.status")
public String updateCatalogue(PositionCatalogue catalogue)
Map<String,List<Catalogue>> map=new HashMap<>();
也可以通过map将数据缓存到内存实现缓存
最终实现
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
yml文件
#指定接入缓存的类型
spring.cache.type=redis
#指定存活时间
spring.cache.redis.time-to-live=3600000
#如果指定了前缀就用我们指定的前缀,如果没有就默认使用缓存的名字作为前缀
#spring.cache.redis.key-prefix=CACHE_
#是否自动分区形成前缀
#spring.cache.redis.use-key-prefix=true
#是否缓存空值,防止缓存穿透 缓存不存在的值
spring.cache.redis.cache-null-values=true
其他知识
/**
* 1、每一个需要缓存的数据我们都来指定要放到那个名字的缓存。【缓存的分区(按照业务类型分)】
* 2、@Cacheable 代表当前方法的结果需要缓存,如果缓存中有,方法都不用调用,如果缓存中没有,会调用方法。最后将方法的结果放入缓存
* 3、默认行为
* 3.1 如果缓存中有,方法不再调用
* 3.2 key是默认生成的:缓存的名字::SimpleKey::[](自动生成key值)
* 3.3 缓存的value值,默认使用jdk序列化机制,将序列化的数据存到redis中
* 3.4 默认时间是 -1:永不过期
*
* 自定义操作:key的生成
* 1. 指定生成缓存的key:key属性指定,接收一个 SpEl
* 2. 指定缓存的数据的存活时间:配置文档中修改存活时间 ttl
* 3. 将数据保存为json格式: 自定义配置类 MyCacheManager
* <p>
* 4、Spring-Cache的不足之处:
* 1)、读模式
* 缓存穿透:查询一个null数据。解决方案:缓存空数据
* 缓存击穿:大量并发进来同时查询一个正好过期的数据。解决方案:加锁 ? 默认是无加锁的;使用sync = true来解决击穿问题
* 缓存雪崩:大量的key同时过期。解决:加随机时间。加上过期时间
* 2)、写模式:(缓存与数据库一致)
* 1)、读写加锁。
* 2)、引入Canal,感知到MySQL的更新去更新Redis
* 3)、读多写多,直接去数据库查询就行
* <p>
* 总结:
* 常规数据(读多写少,即时性,一致性要求不高的数据,完全可以使用Spring-Cache):写模式(只要缓存的数据有过期时间就足够了)
* 特殊数据:特殊设计
* <p>
* 原理:
* CacheManager(RedisCacheManager)->Cache(RedisCache)->Cache负责缓存的读写
*
* @return
*/
举例
@Cacheable(value = {"category"}, key = "#root.method.name", sync = true)
自定义RedisCacheConfiguration 将缓存的数据序列化成json格式
@EnableConfigurationProperties(CacheProperties.class)
@Configuration
@EnableCaching
public class SpringbootRedisConfiguration {
/**
* 配置文件的配置没有用上
* 1. 原来和配置文件绑定的配置类为:@ConfigurationProperties(prefix = "spring.cache")
* public class CacheProperties
* <p>
* 2. 要让他生效,要加上 @EnableConfigurationProperties(CacheProperties.class)
*/
@Bean
public 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;
}
}