Spring Cache+Redis实现缓存数据的一致性

Spring Cache是一个非常优秀的组件。来自Spring3.1,提供了类似于@Transactional注解事务的注解Cachae支持,且提供了Cache抽象,方便切换各种底层Cache(如Redis)

使用Spirng Cache的好处:

  1. 提供基本的Cache抽象,方便切换各种底层Cache;
  2. 通过注解Cache可以实现类似于事务一样,缓存逻辑透明的应用到我们的业务代码上,且只需要更少的的代码就可以完成
  3. 提供事务回滚时也可以自动回滚缓存
  4. 支持比较复杂的缓存逻辑

项目集成Spring Cache+Redus

相关依赖:



    org.springframework.boot
    spring-boot-starter-data-redis




    org.apache.commons
    commons-pool2
    2.6.0

如何使用?

1.在启动项上/自定义的Redis配置文件RedisConfig上添加@EnableCaching注解

@EnableCaching :开启缓存,并配置Redis缓存管理器,@EnableCaching注释出发后置处理器,检查每一个Spring beand public方法是否开启缓存注解,如果找到这样一个注释,自动创建一个代理拦截方法调用和处理相应的缓存行为。

2.在配置文件中添加redis配置

#连接的ip
spring.redis.host=192.168.44.165
#使用端口
spring.redis.port=6379
#使用的数据库编号0~15,16个库
spring.redis.database= 0
#超时时间5分钟 1800000秒
spring.redis.timeout=1800000
#最大连接数
spring.redis.lettuce.pool.max-active=20
#最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-wait=-1
#最大空闲时间 5s
spring.redis.lettuce.pool.max-idle=5
#最小空闲时间 0
spring.redis.lettuce.pool.min-idle=0

3.常用缓存注解

@Cacheable

下次请求时,如果缓存存在,则直接读取缓存数据返回;如果缓存不存在,执行方法,并将返回结果存入缓存中。一般用于查询的方法上

属性值:

value 缓存名,必填,制定了缓存放在那块命名空间
cacheNames 与value差不多,二选一即可
keyGenerator 指定key生成的策略,和key只能使用一个
key 缓存的key,默认为空,可以使用#参数 将参数作为缓存的key值,和keyGenerator只能使用一个
    @Cacheable(value = "dicttest",keyGenerator = "keyGenerator")
    public List findChildDta(long id) {
        ...
    }

执行包含该注解的方法后产生的的缓存对象 

127.0.0.1:6379> keys *
1) "dicttest::com.atguigu.yygh.cmn.service.DictServiceImplfindChildDta1"
2) "dict::com.atguigu.yygh.cmn.service.DictServiceImplfindChildDta1"
3) "dict::com.atguigu.yygh.cmn.service.DictServiceImplfindChildDta0"
127.0.0.1:6379>

 其中缓存的key值由两部分组成:value::key

@CachePut

使用该注解的方法,每次都会执行,并将结果存在指定的缓存中,其他方法可以直接从响应的缓存中读取数据,而不需要再去查询数据,一般用在新增方法上。

属性值

value 缓存名,必填,制定了缓存放在那块命名空间
cacheNames 与value差不多,二选一即可
keyGenerator 指定key生成的策略,和key只能使用一个
key 缓存的key,默认为空,可以使用#参数 将参数作为缓存的key值,和keyGenerator只能使用一个

@CacheEvict

使用该注解的方法,会清空指定的缓存,一般用在更新或删除方法上

属性值

value 缓存名,必填,制定了缓存放在那块命名空间
cacheNames 与value差不多,二选一即可
keyGenerator 指定key生成的策略,和key只能使用一个
key 缓存的key,默认为空,可以使用#参数 将参数作为缓存的key值,和keyGenerator只能使用一个
allEntries 是否清空所有缓冲,默认为false,如果为true,则在方法调用后清空所有的缓存
beforeInvocation 是否在方法执行前就清空,默认为false,如果只定位true,则在方法执行前就会清空缓存
/**
 * 根据上级id获取子节点数据列表
 * @param parentId
*/
@Cacheable(value = "dict",keyGenerator = "keyGenerator")
@Override
public List findByParentId(Long parentId) {
       List dictList = dictMapper.selectList(
            new QueryWrapper().eq("parent_id", parentId));
       dictList.stream().forEach(dict -> {
            boolean isHasChildren = this.isHasChildren(dict.getId());
           dict.setHasChildren(isHasChildren);
       });
return dictList;
}
/**
 * 导入
 * allEntries = true: 方法调用后清空所有缓存
 * @param file
*/
@CacheEvict(value = "dict", allEntries=true)
@Override
public void importData(MultipartFile file) {
   ExcelHelper fileHelper = new ExcelHelper(DictEeVo.class);
   List dictVoList = fileHelper.importExcel(file);
   if(!CollectionUtils.isEmpty(dictVoList)) {
        dictMapper.insertBatch(dictVoList);
   }
}

你可能感兴趣的:(redis,redis,spring,缓存)