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
相关产品推荐
相关产品推荐

