我正在尝试实现回写缓存。我正在尝试使用软引用,但在执行事后回写时遇到问题,因为引用在添加到 gcQueue 之前已被清除,因此我无法访问引用对象。
解决方案?
最佳答案
例子:
final ConcurrentMap<Long, Integer> cache = new MapMaker()
.softValues().expiration(20,TimeUnit.MINUTES)
.makeComputingMap(new Function<Long, Integer>() {
@Override
public Integer apply(Long arg0) {
return null;
}
});
关于 MapMaker 的问题:
备选方案:
使用供应商类的 memoizeWithExpiration这也是 guava 库的一部分。
关于java - 在 Java 中实现回写缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4130684/