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?

MrSmith42
  • 9,961
  • 6
  • 38
  • 49
Jim
  • 2,111
  • 4
  • 25
  • 34

1 Answers1

2

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 :

Alternative option :

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

Community
  • 1
  • 1
Emil
  • 13,577
  • 18
  • 69
  • 108