1、简介
Spring 从 3.1 开始定义了 org.springframework.cache.Cache 和 org.springframework.cache.CacheManager 接口来统一不同的缓存技术;
并支持使用 JCache(JSR-107)注解简化我们开发;
Cache 接口为缓存的组件规范定义,包含缓存的各种操作集合;
Cache 接 口 下 Spring 提 供 了 各 种 xxxCache 的 实 现 ; 如 RedisCache , EhCacheCache , ConcurrentMapCache 等;
每次调用需要缓存功能的方法时,Spring 会检查检查指定参数的指定的目标方法是否已 经被调用过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法并缓 存结果后返回给用户。下次调用直接从缓存中获取。
使用 Spring 缓存抽象时我们需要关注以下两点;
1、确定方法需要被缓存以及他们的缓存策略
2、从缓存中读取之前缓存存储的数据
2、基础概念
`
3、注解
4、表达式语法
5、缓存穿透问题解决
整合springCache
1)引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
2)引入redis依赖,使用redis作为开发场景
3)写配置
自动配置了:CacheAutoConfiguration会导入RedisCacheConfiguration;
自动配好了缓存管理器RedisCacheManager
自己在 application.properties中配置
缓存的各种注释
@Cacheable:触发将数据保存到缓存的操作
@CacheEvice:触发将数据从缓存删除的操作
@CachePut:不影响方法执行更新缓存
@Caching:组合以上多个操作
@CacheConfig:在类级别共享缓存的相同配置
测试使用缓存功能
1)开启缓存功能
在启动类上加@EnableCaching
2)只需要使用注解就能完成缓存操作
//每一个需要缓存的数据我们都来指定放到哪个名字的缓存【缓存的分区(安装业务类型分)】
@Cacheable({"category","product"}) //代表当前结果需要缓存,如果缓存中有,方法不用调用,如果缓存中没有,调用方法,最后将方法的结果放如缓存
@Override
public List<CategoryEntity> getLevel1Categorys() {
List<CategoryEntity> categoryEntities = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));
return categoryEntities;
}
使用该技术缓存的默认行为
1)如果缓存中有,方法不用调用。
2)key默认自动生成,缓存的名字::SimpleKey[ ](自主生成的key值)
3)缓存的value的值,默认使用jdk序列化机制,将序列化后的数据存到redis中
4)默认ttl是-1,永不过时
自定义操作
1)指定key的生成 :key属性指定,接收一个SpEL表达式
2)指定缓存数据的存活时间:在配置文件中修改
3)将数据保存为json格式:编写配置类,还要将配置yml文件中的放到配置类中
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@EnableConfigurationProperties(CacheProperties.class)
@Configuration
@EnableCaching
public class MyCacheConfig {
// @Autowired
// public CacheProperties cacheProperties;
/**
* 配置文件的配置没有用上
* @return
*/
@Bean
public 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 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;
}
}
其他yml配置项
@CacheEvice从缓存中删除数据(失效模式用)
同时进行多种缓存操作
@Caching
删除缓存分区allEntries = true
@CachePut缓存修改,把这次的结果在缓存中放一份(双写模式用)
Spring-Cache的不足
总结:
常规数据(读多写少,即时性,一致性要求不高的数据):可以使用spring—cache;写模式(只要缓存的数据有过期时间就足够了)
特殊数据:特殊设计