1.导入springcache和redis的pom依赖:(redis做缓存)
<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>
</dependency>
2.写配置
2.1 CacheAutoConfiguration回导入RdisCacheConfiguration:自动配好了缓存管理器RedisCacheManager(能力暂时不够,源码看不懂,知道有这么个东西)。
2.2 配置使用redis作为缓存(最简化配置)
spring.cache.type=redis
#缓存值的过期时间
spring.cache.redis.time-to-live=3600000
#是否开启缓存空值,防止缓存穿透
spring.cache.redis.cache-null-values=true
2.3配置redis
spring:
redis:
host: 192.168.56.10
port: 6379
3.常用注解
@Cacheable:触发将数据保存到缓存的操作。
@CAcheEvict:触发将数据从缓存中删除的操作。
@CachePut:不影响方法执行更新缓存。
@Caching:组合以上多个操作。
@CacheConfig:在类级别,共享缓存的相同配置。
3.1 开启缓存功能 在启动类上添加注解:@EnableCacheing
3.2只需使用注解就能完成缓存操作
自定义:
1)指定生成的缓存使用的key:key属性指定,接受一个SpEL表达式
@Cacheable(value = {"category"},key = "'level1Category'")
@Cacheable代表当前的方法返回的结果需要缓存,如果缓存中有,方法不用调用;如果缓存中没有,会调用方法,将最后的结果放入缓存。每一个需要缓存的数据我们都来指定要放入哪个名字的缓存【缓存的分区,j建议按照业务类型来分】,可以是数组@Cacheable({"category","product","product2"})。key = "'level1Category'",指定生成缓存的key值。
2)指定缓存数据的存活时间:在配置文件中配置,单位ms。
spring.cache.redis.time-to-live=3600000
3)将数据保存为JSON格式
自定义配置类,自定义一个缓存管理器
@Configuration
@EnableCaching
public class MyCacheConfig {
@Bean
RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
// RedisCacheConfiguration redisCacheConfiguration = config.entryTtl();
//自定义配置覆盖原来的配置
config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
//将配置文件中的所有配置都生效
CacheProperties.Redis redis = cacheProperties.getRedis();
if (redis.getTimeToLive() != null){
config = config.entryTtl(redis.getTimeToLive());
}
if (redis.getKeyPrefix() != null){
config = config.prefixKeysWith(redis.getKeyPrefix());
}
if (!redis.isCacheNullValues()){
config = config.disableCachingNullValues();
}
if (!redis.isUseKeyPrefix()){
config = config.disableKeyPrefix();
}
return config;
}
}
使用:使用@Cacheable注解,会将返回的结果自动存入redis中,分区是指定的cache,key是指定的cache_hello。如果缓存中有这个数据,这不会调用此方法,直接走redis缓存中取值(这个地方不是很确定)。可以添加一个本地锁(sync -true),防止缓存击穿(大量并发进来同时查询一个正好过期的数据)
//每一个需要缓存的数据我们都来指定要放入哪个名字的缓存【缓存的分区,按照业务类型来分】,可以是数组 @Cacheable({"cache","product","product2"})
@Cacheable(value = {"cache"},key = "'cache_hello'") //代表当前的方法返回的结果需要缓存,如果缓存中有,方法不用调用,如果缓存中没有,会调用方法,将最后的结果放入缓存。
@Override
public List<Hello> hello() {
.......
return list;
}
@CacheEvict触发将数据从缓存中删除的操作。当updateCasecode方法执行后会触发删缓存操作。@Caching组合注解操作。
//@CacheEvict(value = "Cache",key = "'Cache_hello'") // 失效模式、;保证数据一致性。删除对应分区的对应的缓存
@Caching(evict = {
@CacheEvict(value = "Cache",key = "'Cache_hello'"),
@CacheEvict(value = "Cache2",key = "'Cache_hello'")
})
// @Caching(value = "Cache",allEntries = true) //删除指定的"Cache"分区所有数据
@Transactional
@Override
public void updateCascode(Hello hello) {
this.updateById(hello.getID());
}
多看源码!!!!