前言
在以前的编程中,我们如果去使用缓存,一般都是先通过缓存去取数据,如果没有数据,就去数据库查询,然后将查询到的数据缓存到redis中,以便后续直接去缓存中获取数据。这个过程很合理,但是却是极其重复性的工作,最近又看到SpringCache发现有了更简洁、优雅的实现方式。
基础概念
常用注解
方法参数
EL表达式
一、引入依赖
<!--引入springcache依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
二、配置文件
# 配置使用cache类型
spring.cache.type=redis
# 配置redis缓存有效期,不配置的话默认永久
spring.cache.redis.time-to-live=3600000
# 配置是否使用缓存前缀
spring.cache.redis.use-key-prefix=true
# 配置缓存前缀,不指定则使用缓存的名字作为前缀
spring.cache.redis.key-prefix=cache:
# 配置是否缓存空值,缓存空值可以避免缓存穿透
spring.cache.redis.cache-null-values=true
# 配置是否开启缓存统计
spring.cache.redis.enable-statistics=false
三、自定义cachemanager
import org.springframework.beans.factory.annotation.Autowired;
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
// CacheProperties cacheProperties;
/**
* 配置文件中的东西没有用上
*
* 1、原来和配置文件绑定的配置类是这样子的
* @ConfigurationProperties(prefix = "spring.cache")
* public class CacheProperties
*
* 2、要让他生效
* @EnableConfigurationProperties(CacheProperties.class)
*
* @return
*/
@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()));
//将配置文件中的所有配置都让它生效
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;
}
}
三、使用示例
3.1 删除缓存分区的数据
/**
* 查询一级分类
* @return
*/
@Override
@Cacheable(value = {"category"}, key = "'level1Categories'")
public List<CategoryEntity> findLevel1Categories() {
}
/**
* 查询全部分类
* 使用SpringCache缓存版本
* 只需要操作数据库,不需要关心缓存,一个注解就够了
* @return
*/
@Cacheable(value = {"category"}, key = "'categoryJson'")
@Override
public Map<Long, List<Category2VO>> getCategoryJson() {
// 查出所有分类
List<CategoryEntity> allCategories = this.list();
List<CategoryEntity> l1Categories = listByPrentCid(allCategories, 0L);
Map<Long, List<Category2VO>> categoryMap = l1Categories.stream().collect(Collectors.toMap(k1 -> k1.getCatId(), v1 -> {
List<CategoryEntity> l2Categories = listByPrentCid(allCategories, v1.getCatId());
List<Category2VO> category2VOs = null;
if (l2Categories != null && l2Categories.size() > 0) {
category2VOs = l2Categories.stream().map(l2 -> {
// 根据当前2级分类查出所有3级分类
List<CategoryEntity> l3Categories = listByPrentCid(allCategories, l2.getCatId());
List<Category3VO> category3VOs = null;
if (l3Categories != null && l3Categories.size() > 0) {
category3VOs = l3Categories.stream().map(l3 -> new Category3VO(l2.getCatId(),
l3.getCatId(), l3.getName())).collect(Collectors.toList());
}
return new Category2VO(v1.getCatId(), category3VOs, l2.getCatId(), l2.getName());
}).collect(Collectors.toList());
}
return category2VOs;
}));
return categoryMap;
}
3.1.2 删除一个分区的缓存数据
// 删除一个分区的缓存数据
@CacheEvict(value = "category", key = "'level1Categories'")
public void updateDetail(CategoryEntity category) {
}
3.1.3 删除多个分区的缓存数据
// 删除多个分区的缓存数据
@Caching(evict = {
@CacheEvict(value = "category", key = "'level1Categories'"),
@CacheEvict(value = "category", key = "'categoryJson'"),
})
3.1.4 删除指定分区的缓存数据
// 删除某个分区下的缓存数据,也就是清除模式
@CacheEvict(value = "category",allEntries = true)
// 如果希望修改完数据,再往缓存里放一份,也就是双写模式,可以使用这个注解
@CachePut
评论 (0)