一、缓存
为了系统性能的提升,我们一般都会将部分数据放入缓存中,加速访问。而 db
承担数据落盘工作。哪些数据适合放入缓存?举例:电商类应用,商品分类,商品列表等适合缓存并加一个失效时间(根据数据更新频率来定),后台如果发布一个商品,买家需要 5 分钟才能看到新的商品一般还是可以接受的。
- 即时性、数据一致性要求不高的
- 访问量大且更新频率不高的数据(读多,写少)
注意:在开发中,凡是放入缓存中的数据我们都应该指定过期时间,使其可以在系统即使没有主动更新数据也能自动触发数据加载进缓存的流程。避免业务崩溃导致的数据永久不一致问题。
1、最简单的缓存Map 本地缓存
2、整合Redis作为缓存
二、缓存失效问题
1、缓存穿透
缓存穿透是指查询一个一定不存在的数据,由于缓存是不命中,将去查询数据库,但是数据库也无此记录,我们没有将这次查询的 null写入缓存,这将导致这个不存在的数据每次请求都要到存储层去查询,失去了缓存的意义。在流量大时,可能 DB 就挂掉了,要是有人利用不存在的 key 频繁攻击我们的应用,这就是漏洞。
解决: 缓存空结果、并且设置短的过期时间。
2、缓存雪崩
缓存雪崩是指在我们设置缓存时采用了相同的过期时间,导致缓存在某一时刻同时失效,请求全部转发到 DB,DB 瞬时压力过重雪崩。
解决: 原有的失效时间基础上增加一个随机值,比如 1-5 分钟随机,这样每一个缓存的过期时间的重复率就会降低,就很难引发集体失效的事件。
3、缓存击穿
对于一些设置了过期时间的 key,如果这些 key可能会在某些时间点被超高并发地访问,是一种非常“热点”的数据。这个时候,需要考虑一个问题:如果这个 key
在大量请求同时进来前正好失效,那么所有对这个 key 的数据查询都落到 db,我们称为缓存击穿。
解决: 加锁
public Map<String, List<Category2Vo>> getCategoryJsonFromDbWithLocalLock() {
/**
* 只要是同一把锁 就能锁住所有线程
* synchronized (this) springboot 所有组件在容器中都是单例的
* todo 本地锁 synchronized JUC(LOCK) 在分布式情况下 必须使用分布式锁
*/
synchronized (this) {
/**
* @Description 优化 将对数据库的多次循环查询变为一次
* 得到锁以后 去缓存中确定一次 没有才继续查询
*/
return getDataFromDb();
}
}
三、缓存数据一致性



四、Spring Cache
1、引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
2、配置reids作为缓存
spring.cache.type=redis
spring.cache.redis.time-to-live=3600000
#缓存空值 防止缓存穿透
spring.cache.redis.cache-null-values=true
#spring.cache.redis.key-prefix=CACHE_
#是否使用前缀
spring.cache.redis.use-key-prefix=true
3、开启缓存
@EnableCaching
public class GulimallProductApplication {}
4、测试使用
@Cacheable 触发将数据保存到缓存的操作
@CacheEvict 触发将数据从缓存删除的操作
@CachePut 不影响方法执行更新缓存
@Caching 组合以上多个操作
@CacheConfig 在类级别共享缓存的相同配置
//每一个需要缓存的数据 指定分区 (最好按照业务类型分)
//代表当前方法需要缓存 如果缓存有 方法不用调用 如果没有 调用方法最后将方法结果放入缓存
//
@Cacheable(value = {"category"},key = "#root.method.name",sync = true)
public List<CategoryEntity> getLevel1Categorys(){}

修改配置
@EnableConfigurationProperties(CacheProperties.class)
@Configuration
@EnableCaching
public class MyCacheConfig {
@Bean
RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
//config = config.entryTtl();
config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericFastJsonRedisSerializer()));
CacheProperties.Redis redisProperties = cacheProperties.getRedis();
if (redisProperties.getTimeToLive() != null) {
config = config.entryTtl(redisProperties.getTimeToLive());
}
if (redisProperties.getKeyPrefix() != null) {
config = config.prefixCacheNameWith(redisProperties.getKeyPrefix());
}
if (!redisProperties.isCacheNullValues()) {
config = config.disableCachingNullValues();
}
if (!redisProperties.isUseKeyPrefix()) {
config = config.disableKeyPrefix();
}
return config;
}
}
//"'getCategoryJson'" 普通字符串加 ''
@CachePut 双写模式
//失效模式 删除category分区下所有的缓存 和@Caching都可
@CacheEvict(value = {"category"},allEntries = true)
@Caching(evict = { @CacheEvict(value = {"category"},key = "'getLevel1Categorys'"),
@CacheEvict(value = {"category"},key = "'getCategoryJson'")
})
不足
//解决缓存击穿 加锁 本地锁 够用 100个服务也就放100个进来
@Cacheable(sync = true)

版权声明:本文为weixin_40566934原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。
