Java 未调用guava cache RemovalListener
我使用以下参数创建了一个缓存:Java 未调用guava cache RemovalListener,java,caching,guava,Java,Caching,Guava,我使用以下参数创建了一个缓存: cacheTempFiles=CacheBuilder.newBuilder() { @凌驾 移除时的公共无效(移除通知) { deleteTemporaryFile(notification.getValue()); } }).build(); 此外,我每2分钟调用一次cacheTempFiles.cleanUp()。但是,似乎从未调用过onremovation 我的实现中缺少了什么?它肯定会工作,请参见下面的示例: @Test public void sho
cacheTempFiles=CacheBuilder.newBuilder()
{
@凌驾
移除时的公共无效(移除通知)
{
deleteTemporaryFile(notification.getValue());
}
}).build();
此外,我每2分钟调用一次cacheTempFiles.cleanUp()代码>。但是,似乎从未调用过onremovation
我的实现中缺少了什么?它肯定会工作,请参见下面的示例:
@Test
public void shouldCallRemovalListener() {
AtomicInteger counter = new AtomicInteger();
MutableClock clock = MutableClock.epochUTC();
Ticker ticker = new Ticker() {
@Override
public long read() {
return TimeUnit.MILLISECONDS.toNanos(clock.millis());
}
};
Path tmpPath = Path.of("/tmp");
Cache<String, Path> cacheTempFiles = CacheBuilder.newBuilder()
.ticker(ticker)
.maximumSize(250)
.expireAfterWrite(15, TimeUnit.SECONDS)
.removalListener(
(RemovalNotification<String, Path> notification) ->
System.out.println(String.format(
"Delete '%s -> %s' (%d times)",
notification.getKey(), notification.getValue(), counter.incrementAndGet())))
.build();
cacheTempFiles.put("tmp", tmpPath);
assertThat(cacheTempFiles.asMap()).containsOnly(Assertions.entry("tmp", tmpPath));
assertThat(counter).hasValue(0);
clock.add(Duration.ofSeconds(20));
cacheTempFiles.cleanUp();
assertThat(cacheTempFiles.asMap()).isEmpty();
assertThat(counter).hasValue(1);
}
@测试
public void shouldCallRemovalListener(){
AtomicInteger计数器=新的AtomicInteger();
MutableClock=MutableClock.epochUTC();
股票代码=新股票代码(){
@凌驾
公众长期阅读(){
返回TimeUnit.millises.toNanos(clock.millis());
}
};
路径tmpPath=Path.of(“/tmp”);
Cache cacheTempFiles=CacheBuilder.newBuilder()
.ticker(股票代码)
.最大尺寸(250)
.expireAfterWrite(15,时间单位为秒)
.removalListener(
(移除通知)->
System.out.println(String.format(
“删除'%s->%s'(%d次)”,
notification.getKey()、notification.getValue()、counter.incrementAndGet())
.build();
cacheTempFiles.put(“tmp”,tmpPath);
assertThat(cacheTempFiles.asMap())。只包含(Assertions.entry(“tmp”,tmpPath));
资产(计数器).hasValue(0);
时钟加(持续时间秒(20));
cacheTempFiles.cleanUp();
断言(cacheTempFiles.asMap()).isEmpty();
资产(计数器).hasValue(1);
}
通过并输出
Delete'tmp->/tmp'(1次)
您如何确定从未调用过OnRemove?eclipse中的调试点和OnRemove中被调用方法的日志输出。