我的配置:
@Bean
public CaffeineCacheManager cacheManager() {
return new CaffeineCacheManager();
}
@Bean
public CaffeineCache testCache() {
return new CaffeineCache("test_cache",
Caffeine.newBuilder()
.maximumSize(10000)
.expireAfterAccess(30, TimeUnit.SECONDS)
.expireAfterWrite(30, TimeUnit.SECONDS)
.recordStats()
.build());
}
测试代码:(连续读取缓存 3 次,读取间隔 45 秒)
static int value = 1;
...
Cache testCache = cacheManager.getCache("test_cache");
System.out.println("read " + testCache.get("myKey", () -> value++));
try {
Thread.sleep(45000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("read " + testCache.get("myKey", () -> value++));
try {
Thread.sleep(45000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("read " + testCache.get("myKey", () -> value++));
实际结果:
read 1
read 1
read 1
预期结果:缓存在 30 秒后被驱逐:
read 1
read 2
read 3
我错了什么?
怎么修 ?