当前位置:   article > 正文

Springboot @Cacheable配置自定义过期时间_public cacheconfig(long ttl, long maxidletime)

public cacheconfig(long ttl, long maxidletime)
@Bean
public RedisCacheManager classifyTreeList1d(RedisConnectionFactory connectionFactory) { //可配置多个

    RedisCacheConfiguration config = instanceConfig(3600 * 24 * 1L); //设置过期时间为1天
    return RedisCacheManager.builder(connectionFactory)
            .cacheDefaults(config)
            .transactionAware()
            .build();
}

private RedisCacheConfiguration instanceConfig(Long ttl) {

    Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    objectMapper.registerModule(new JavaTimeModule());
    // 去掉各种@JsonSerialize注解的解析
    objectMapper.configure(MapperFeature.USE_ANNOTATIONS, false);
    // 只针对非空的值进行序列化
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    // 将类型序列化到属性json字符串中
    objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);

    jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
    return RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofSeconds(ttl))
            .disableCachingNullValues()
            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

使用

@CachePut(value = {"classifyTreeList"}, key = "#classify.getClassifyId()", cacheManager = "classifyTreeList1d")
  • 1
@Cacheable(value = {"classifyTreeList"}, key = "#queryReq.getParameter()", cacheManager = "classifyTreeList1d")
  • 1

删除单个key

不删除value下所有数据,而删除value里面某一条数据,通过key字段进行删除

在这里插入图片描述

    /**
     * 前台_分类刷题_查看分类下试题
     * @author kfj
     * @return ApiResult
     */
    @Override
    @Cacheable(value = "classifySubjectList", key = "#classifyId", cacheManager = "oneDay")
    public ApiResult<List<TotalSubject>> getCountByClassifyId(Long classifyId) {
        System.out.println("============================");
        QueryWrapper<TotalSubject> queryWrapper = new QueryWrapper<TotalSubject>();
        queryWrapper.in("classify_id",classifyId);
        List<TotalSubject> totalSubjects = totalSubjectMapper.selectList(queryWrapper); //根据分类id查询所有试题

        return ApiResult.success(totalSubjects);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
 /**
     *  新增保存考试试题总
     */
    @Override
    @CacheEvict(value = {"totalSubjectList"}, allEntries=true)
    public ApiResult totalSubjectSave(TotalSubject totalSubject, UserRoot user) {

        try {
            if("1".equals(totalSubject.getIsRelease())){
                totalSubject.setReleaseTime(new Date());
            }
            if(totalSubject.getClassifyId() != null){
                Classify classify = classifyMapper.selectById(totalSubject.getClassifyId());
                totalSubject.setClassifyName(classify.getClassifyName());
            }
            totalSubject.setCreateBy(user.getRealName());
            totalSubject.setCreateTime(new Date());
            super.save(totalSubject);
            classifyService.updateClassifyCache(totalSubject.getClassifyId());
            return ApiResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.defaultError(null);
        }
    }

    /**
     * 修改保存考试试题总
     */
    @Override
    @CacheEvict(value = {"totalSubjectList"}, allEntries=true)
    public ApiResult totalSubjectUpdate(TotalSubject totalSubject, UserRoot user) {

        try {
            if(totalSubject.getSubjectId() != null) {
                if("1".equals(totalSubject.getIsRelease())){
                    totalSubject.setReleaseTime(new Date());
                }
                if(totalSubject.getClassifyId() != null){
                    Classify classify = classifyMapper.selectById(totalSubject.getClassifyId());
                    totalSubject.setClassifyName(classify.getClassifyName());
                }

                totalSubject.setUpdateBy(user.getRealName());
                totalSubject.setUpdateTime(new Date());
                super.update(totalSubject);
                classifyService.updateClassifyCache(totalSubject.getClassifyId());
                return ApiResult.success();
            }
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.defaultError(null);
        }
        return ApiResult.fail(MsgConstant.EXCEPTION_ID_NULL.getMsg());
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
    @CacheEvict(value="classifySubjectList", key = "#classify",allEntries=false)
    public void updateClassifyCache(long classify) {

    }
  • 1
  • 2
  • 3
  • 4
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/353156
推荐阅读
相关标签
  

闽ICP备14008679号