2

I trying to implement a write-back cache. I'm trying to use soft referenes, but I'm having troubles performing the post-mortum write-back because the reference is cleared before it's added to the gcQueue and thus I don't have access to the referent object.

Solutions?

4

1 回答 1

2

你可以试试Guava 的 Mapmaker

例子:

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 的问题:

替代选项:

使用 Supplier 类的memoizeWithExpiration,它也是 guava 库的一部分。

于 2010-11-09T10:31:10.657 回答