【发布时间】:2011-05-07 01:14:56
【问题描述】:
我尝试实现回写式缓存。我正在尝试使用软引用,但在执行死后回写时遇到了麻烦,因为引用在添加到 gcQueue 之前已被清除,因此我无权访问引用对象。
解决方案?
【问题讨论】:
标签: java caching soft-references
我尝试实现回写式缓存。我正在尝试使用软引用,但在执行死后回写时遇到了麻烦,因为引用在添加到 gcQueue 之前已被清除,因此我无权访问引用对象。
解决方案?
【问题讨论】:
标签: java caching soft-references
例子:
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 的 SO 问题:
备选方案:
使用 Supplier 类的 memoizeWithExpiration,它也是 guava 库的一部分。
【讨论】: