开发者

Implementing a Write-Back Cache in Java

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 referenc开发者_如何学JAVAe is cleared before it's added to the gcQueue and thus I don't have access to the referent object.

Solutions?


You can try Guava's Mapmaker.

Example:

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;
            }
            });

SO Questions on MapMaker :

  • Use of Google-collections MapMaker ?
  • Using MapMaker to create a cache

Alternative option :

Use Supplier class's memoizeWithExpiration which is also part of guava library.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜