1.dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.配置redis地址
redis:
host: 192.168.73.10
port: 6379
3.分布式锁
(1)自己的锁,自己删除
(业务时间过长,导致锁过期,然后误删别人的锁,导致别的线程获取到锁)
(2)设置过期时间 (解决死锁问题)
(宕机了,导致其他线程一直阻塞在获取锁那块)
(3)业务时间过长,导致锁过期。(优化业务)
(4)设置锁时,要有原子性,过期时间一起设置上,否则可能死锁
(5)删除锁时,比对加删除,原子删除(否则可能删除别人的锁)
4.redisson实现分布式锁
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.13.4</version>
</dependency>
这个用作连续,后面可以使用redisson-spring-boot-starter
(1)lock锁
不设置时间,会自动设置时间为30s
其中每10秒自动更新时间,无限延期,保证业务必定执行完
设置时间,不会自动延期
(2)countdownLock
闭锁,一般用于任务等待另一个任务的完成
(3)读写锁 ,只要有写锁,就会有限制
读读不限
(4)信号量
可以用于限流
5.缓存一致性问题
(1)双写 更改时,缓存也重新更改下。(有脏数据问题,1线程被后面的2线程后来居上了,先改了缓存,然后缓存的数据又被1改了。)
允许暂时的脏数据,数据缓存设置过期时间
(2)失效模式(用时再查)更改时,删除缓存 问题:
(3)canal中间件
canal可以节省大量计算时间,可以一次计算,多次使用,过期之后,再计算一次
最终解决
根据系统选择:失效模式,缓存设置过期时间(最终一致性)。读写数据时,加上分布式读写锁
7.springcache
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
指定缓存类型并在主配置类上加上注解@EnableCaching
spring:
cache:
#指定缓存类型为redis
type: redis
redis:
# 指定redis中的过期时间为1h
time-to-live: 3600000
默认使用jdk进行序列化(可读性差),默认ttl为-1永不过期,自定义序列化方式需要编写配置类
将CacheProperties加入到容器中
@EnableConfigurationProperties(CacheProperties.class)
@Configuration
public class MyCacheConfig {
@Bean
public RedisCacheConfiguration redisCacheConfiguration( CacheProperties cacheProperties) {
CacheProperties.Redis redisProperties = cacheProperties.getRedis();
org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration
.defaultCacheConfig();
//指定缓存序列化方式为json
config = config.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
//设置配置文件中的各项配置,如过期时间
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;
}
}
3)缓存使用@Cacheable@CacheEvict
第一个方法存放缓存,第二个方法清空缓存
// 调用该方法时会将结果缓存,缓存名为category,key为方法名
// sync表示该方法的缓存被读取时会加锁 // value等同于cacheNames // key如果是字符串"''"
@Cacheable(value = {"category"},key = "#root.methodName",sync = true)
public Map<String, List<Catalog2Vo>> getCatalogJsonDbWithSpringCache() {
return getCategoriesDb();
}
//调用该方法会删除缓存category下的所有cache,如果要删除某个具体,用key="''"
@Override
@CacheEvict(value = {"category"},allEntries = true)
public void updateCascade(CategoryEntity category) {
this.updateById(category);
if (!StringUtils.isEmpty(category.getName())) {
categoryBrandRelationService.updateCategory(category);
}
}
如果要清空多个缓存,用@Caching(evict={@CacheEvict(value="")})
4) SpringCache原理与不足
1)、读模式
缓存穿透:查询一个null数据。解决方案:缓存空数据,可通过spring.cache.redis.cache-null-values=true
缓存击穿:大量并发进来同时查询一个正好过期的数据。解决方案:加锁 ? 默认是无加锁的;
使用sync = true来解决击穿问题
缓存雪崩:大量的key同时过期。解决:加随机时间。
2)、写模式:(缓存与数据库一致)
读写加锁。
引入Canal,感知到MySQL的更新去更新Redis
读多写多,直接去数据库查询就行
3)、总结:
常规数据(读多写少,即时性,一致性要求不高的数据,完全可以使用Spring-Cache):
写模式(只要缓存的数据有过期时间就足够了)
特殊数据:特殊设计