You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Micronaut框架下Caffeine缓存expire-after-write配置不生效问题求助

Micronaut框架下Caffeine缓存expire-after-write配置不生效问题求助

大家好,我在使用Micronaut框架搭配Caffeine缓存时碰到了一个棘手的问题:我配置了expire-after-write为2秒,但缓存并没有在指定时间后自动过期,手动失效缓存是正常的,想请教下各位有没有遇到类似情况或者知道问题出在哪?

先贴一下我的配置文件内容:

micronaut:
  application:
    name: isac-card
  cache:
    redis:
      enabled: false
    caffeine:
      enabled: true
      caches:
        keyConfig:
          maximum-size: 1000
          expire-after-write: 2s

然后是我的测试代码,用来验证缓存过期:

@Test
void testCacheExpiry() {
    System.out.println("First Call: " + cacheService.getValue()); // Fetches from AWS
    System.out.println("Second Call: " + cacheService.getValue()); // Should be cached
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    System.out.println("After 3s: " + cacheService.getValue());
    cacheService.invalidateValue();
}

对应的服务层代码如下:

@Cacheable(value = "keyConfig")
public String getValue() {
    // fetch from db 
    return value;
}

@CacheInvalidate("keyConfig")
public void invalidateValue() {
    System.out.println("Invalidating cache...");
}

现在的问题是:

  • 手动调用invalidateValue()时,缓存可以正常失效,日志会显示Value not found in cache for invocation: String getValue()
  • 但等待3秒(已经超过配置的2秒过期时间)后调用getValue(),日志依然显示Value found in cache [keyConfig] for invocation: String getValue(),说明缓存没有自动过期,还是从缓存取数据。

有没有小伙伴能帮忙分析下问题出在哪呀?

备注:内容来源于stack exchange,提问作者deepika azad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 11:23:06