Spring Cache + Caffeine使用中的坑——缓存数据修改导致缓存Key失效问题

问题描述

今天在项目中突然碰到一个问题:使用的缓存是Spring Cache + Caffeine,缓存在执行两次后,突然Key中定义的条件失效。代码如下:

public class CodeOutputService {
    // 注入当前类的对象
    @Resources
    private CodeOutputService codeOutputService;

    /**
     * 获取所有Leader的信息,如果返回list不为空,则加入缓存
     */
    @Cacheable(cacheNames = {"code_output"}, key = "#root.methodName", unless = "#result == null || #result.size() <= 0")
    public List<String> getLeaderListOfCode() {
        List<String> resultList = new ArrayList<>();
        // 此处省略处理逻辑
        ...
        return resultList;
    }

    /**
     * 获取满足条件的Leader的信息,如果返回list不为空,则加入缓存
     */
    @Cacheable(cacheNames = {"code_output"}, key = "methodName + T(String).valueOf(#showBoss)", unless = "#result == null || #result.size() <= 0")
    public List<String> getLeaderListOfCode(boolean showBoss) 
        // 如果缓存存在,这里会从缓存中获取
        List<String> allLeaderList = this.codeOutputService.getLeade
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值