SpringCache的介绍和使用
1.简介
1)Spring 从 3.1 开始定义了 org.springframework.cache.Cache和 org.springframework.cache.CacheManager 接口来统一不同的缓存技术;
并支持使用 JCache(JSR-107)注解简化我们开发;
2)每次调用需要缓存功能的方法时,Spring 会检查检查指定参数的指定的目标方法是否已
经被调用过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法并缓
存结果后返回给用户。下次调用直接从缓存中获取。
这些介绍,大家看看就行了,我们主要如何使用它
2.基本概念图
3.常用注解
@EnableCaching:主方法中标注,表示开启缓存注解功能
@Cacheable:在方法执行前spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中
@CachePut:也是将方法的返回结果放入到缓存中,和 @Cacheable 不同的是,它每次都会触发真实方法的调用,这个注释可以确保方法被执行,同时方法的返回值也被记录到缓存中,实现缓存与数据库的同步更新。
@CacheEvict:删除缓存
@Caching:组合以上多个操作的
4.使用
1)导入依赖
<!--redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--springcache依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
2)相关配置
application.properties
spring.cache.type=redis
#spring.cache.cache-names=qq,毫秒为单位
spring.cache.redis.time-to-live=3600000
#如果指定了前缀就用我们指定的前缀,如果没有就默认使用缓存的名字作为前缀
#spring.cache.redis.key-prefix=CACHE_
spring.cache.redis.use-key-prefix=true
#是否缓存空值,防止缓存穿透
spring.cache.redis.cache-null-values=true
MyCachingConfig:
/**
* springcach配置
*/
@EnableConfigurationProperties(CacheProperties.class)
@Configuration
public class MyCacheConfig {
// @Autowired
// CacheProperties cacheProperties;
/**
* 需要将配置文件中的配置设置上
* 1、使配置类生效
* 1)开启配置类与属性绑定功能EnableConfigurationProperties
*
* @ConfigurationProperties(prefix = "spring.cache") public class CacheProperties
* 2)注入就可以使用了
* @Autowired CacheProperties cacheProperties;
* 3)直接在方法参数上加入属性参数redisCacheConfiguration(CacheProperties redisProperties)
* 自动从IOC容器中找
* <p>
* 2、给config设置上
*/
@Bean
RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
// 当自己往IOC注入了RedisCacheConfiguration配置类时,以下参数全都失效,需要手动设置
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;
}
}
@EnableCaching:
@Cacheable:
/**
* key是默认生成的:缓存的名字::SimpleKey::[](自动生成key值)
* 缓存的value值,默认使用jdk序列化机制,将序列化的数据存到redis中
* 默认时间是 -1
* @return
* 自定义操作:key的生成
* 1. 指定生成缓存的key:key属性指定,接收一个 SpEl,关于SpEl的写法,可以查看Cacheable注解的注释或官方文档,
* 2. 指定缓存的数据的存活时间:配置文档中修改存活时间 ttl (配置文件已设置)
* 3. 将数据保存为json格式: 自定义配置类 MyCacheManager (配置文件已设置)
* 4.如果key想要设置为字符需要这样写:key=”‘内容’“
* 5.下面的category是分区名(就是有很多种类的缓存,比如品牌的,分类的等)
* 6.下面的key是缓存的名字
*/
@Cacheable(value ="category",key = "#root.methodName")
@Override
public Map<String, List<Catelog2Vo>> getCatalogJson() {
List<CategoryEntity> list = this.list(null);
//查出一级菜单
List<CategoryEntity> categorys = this.list(new QueryWrapper<CategoryEntity>().eq("cat_level", 1));
Map<String, List<Catelog2Vo>> collect1 = categorys.stream().collect(Collectors.toMap((k) -> {
return k.getCatId().toString();
}, (v) -> {
List<CategoryEntity> categoryEntity2 = getParent_cid(list, v.getCatId());
List<Catelog2Vo> catelog2Vos = categoryEntity2.stream().map((obj) -> {
Catelog2Vo catelog2Vo = new Catelog2Vo();
catelog2Vo.setCatalog1Id(v.getCatId().toString());
catelog2Vo.setId(obj.getCatId().toString());
catelog2Vo.setName(obj.getName());
List<CategoryEntity> catelog3Vo = getParent_cid(list, obj.getParentCid());
List<Catelog2Vo.Catalog3Vo> collect = catelog3Vo.stream().map((vo3) -> {
Catelog2Vo.Catalog3Vo catelo3Vo = new Catelog2Vo.Catalog3Vo();
catelo3Vo.setName(vo3.getName());
catelo3Vo.setId(vo3.getCatId().toString());
catelo3Vo.setCatalog2Id(obj.getCatId().toString());
return catelo3Vo;
}).collect(Collectors.toList());
catelog2Vo.setCatalog3List(collect);
return catelog2Vo;
}).collect(Collectors.toList());
return catelog2Vos;
}));
return collect1;
}
缓存结果:
CachePut:这个注解的使用和Cacheable的差不多,要注意他们的区别
@CacheEvict:删除category分区下的key为getCatalogJson,一般使用在修改方法,注意单引号
@Caching
举个例子。我们要删除category分区下的key为one和key为two的两个key,怎么办呢?
第一种写法:
第二种写法:注意这是删除category分区下的所有key
希望对你有帮助,有问题私信一起交流!