开发者

Implementing a Write-Back Cache in Java

开发者 https://www.devze.com 2023-01-23 22:43 出处:网络
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\'

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

精彩评论

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

关注公众号