这几天学习谷粒商城又再次的回顾了一次SpringCache,之前在学习谷粒学院的时候其实已经学习了一次了!!!
这里就对自己学过来的内容进行一次的总结和归纳!!!
一、什么是SpringCache-
Spring Cache 是一个非常优秀的缓存组件。自Spring 3.1起,提供了类似于@Transactional注解事务的注解Cache支持,且提供了Cache抽象,方便切换各种底层Cache(如:redis)
-
使用Spring Cache的好处:
- 提供基本的Cache抽象,方便切换各种底层Cache;
- 通过注解Cache可以实现类似于事务一样,缓存逻辑透明的应用到我们的业务代码上,且只需要更少的代码就可以完成;
- 提供事务回滚时也自动回滚缓存;
- 支持比较复杂的缓存逻辑;
二、项目集成Spring Cache + Redis
- 依赖
org.springframework.boot spring-boot-starter-data-redis org.springframework.boot spring-boot-starter-cache
开启@EnableCaching
1、配置方式 ①第一种:配置类@Configuration
@EnableCaching
public class RedisConfig {
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
@Bean
public RedisTemplate
或
②结合配置+配置文件spring:
cache:
#指定缓存类型为redis
type: redis
redis:
# 指定redis中的过期时间为1h
time-to-live: 3600000
key-prefix: CACHE_ #缓存key前缀
use-key-prefix: true #是否开启缓存key前缀
cache-null-values: true #缓存空值,解决缓存穿透问题
默认使用jdk进行序列化(可读性差),默认ttl为-1永不过期,自定义序列化方式为JSON需要编写配置类
@Configuration
@EnableConfigurationProperties(CacheProperties.class)//拿到Redis在配置文件的配置
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;
}
}
说明:
- 第一种,全自定义配置
- 第二种,简单配置+自定义配置value值json转义
- 对redis进行配置
#redis配置 spring.redis.host=47.120.237.184 spring.redis.port=6379 spring.redis.password=ach2ng@123356 spring.redis.database= 0 spring.redis.timeout=1800000 #redis池设置 spring.redis.lettuce.pool.max-active=20 spring.redis.lettuce.pool.max-wait=-1 #最大阻塞等待时间(负数表示没限制) spring.redis.lettuce.pool.max-idle=5 spring.redis.lettuce.pool.min-idle=0
三、使用Spring Cache
- @Cacheable
根据方法对其返回结果进行缓存,下次请求时,如果缓存存在,则直接读取缓存数据返回;如果缓存不存在,则执行方法,并把返回的结果存入缓存中。一般用在查询方法上。
| 属性/方法名 | 解释 |
|---|---|
| value | 缓存名,必填,它指定了你的缓存存放在哪块分区,可多指定,如{"catagory","xxxx",....} |
| cacheNames | 与 value 差不多,二选一即可 |
| key | 可选属性,可以使用 SpEL 标签自定义缓存的key,如:#root.methodName【用方法名作为key】 |
| sync | 默认false,为true时,会让操作被同步保护,可避免缓存击穿问题 |
- @CachePut
使用该注解标志的方法,每次都会执行,并将结果存入指定的缓存中。其他方法可以直接从响应的缓存中读取缓存数据,而不需要再去查询数据库。一般用在新增方法上。
| 属性/方法名 | |
|---|---|
| value | 缓存名,必填,它指定了你的缓存存放在==哪块分区,==可多指定,如{"catagory","xxxx",....} |
| cacheNames | 与 value 差不多,二选一即可 |
| key | 可选属性,可以使用 SpEL 标签自定义缓存的key |
- @CacheEvict
使用该注解标志的方法,会清空指定的缓存。一般用在更新或者删除方法上
| 属性/方法名 | 解释 |
|---|---|
| value | 缓存名,必填,它指定了你的缓存存放在==哪块分区,==可多指定,如{"catagory","xxxx",....} |
| cacheNames | 与 value 差不多,二选一即可 |
| key | 可选属性,可以使用 SpEL 标签自定义缓存的key key如果是字符串"’’",【请加上单引号】 |
| allEntries | 是否清空所有缓存,默认为 false。如果指定为 true,则方法调用后将立即清空所有的缓存 allEntries = true,会对删除value分区里的所有数据 |
| beforeInvocation | 是否在方法执行前就清空,默认为 false。如果指定为 true,则在方法执行前就会清空缓存 |
- @Caching
可组合使用以上注解,如:
//组合了删除缓存的@CacheEvict注解,同时删除两个
@Cacheing(evict=
{@CacheEvict(value="a"),key="'getLists'"}),
{@CacheEvict(value="b"),key="'getArr'"})
)
- 使用举例
- 失效模式:
- 更新操作后,删除缓存
- 双写模式:
- 更新操作后,新增缓存,掩盖
- 失效模式:
四、SpringCache原理与不足 1、读模式
-
缓存穿透:
查询一个null数据。
- 解决方案:缓存空数据,
- 可通过spring.cache.redis.cache-null-values=true
- 解决方案:缓存空数据,
-
缓存击穿:
大量并发进来同时查询一个正好过期的数据。
- 解决方案:
- 加锁 ? 默认是无加锁的;
使用sync = true来解决击穿问题
- 加锁 ? 默认是无加锁的;
- 解决方案:
-
缓存雪崩:
大量的key同时过期。
- 解决方案:加随机时间,time-to-live: 3600000。
-
读写加锁。
-
引入Canal,感知到MySQL的更新去更新Redis
-
读多写多,直接去数据库查询就行
五、总结
-
常规数据(读多写少,即时性,一致性要求不高的数据,完全可以使用Spring-Cache):
- 写模式(只要缓存的数据有过期时间就足够了)
-
特殊数据:
- 特殊设计(读写锁、redis分布式锁【redisson】等)



