Java Guava combination of Multimap and Cache

2019-04-04 09:09发布

Is there any such thing as a combination of Guava's Cache and Multimap functionality available? Essentially, I need a collection where entries expire after a given time such as available in Cache but I have non-unique keys and I need the entries to expire independently.

3条回答
倾城 Initia
2楼-- · 2019-04-04 09:27

As long as you're talking about Cache and not LoadingCache you could pass the Cache.asMap() view into Multimaps.newMultimap.

查看更多
SAY GOODBYE
3楼-- · 2019-04-04 09:30

With a Guava Cache there is no put method, the cache is designed to be self-populating. The values returned from a key lookup are calculated at runtime. A similar approach is taken by Commons Collections Transformer Factories.

I think you could implement what you are looking for quite easily. If you look at a simple Map backed example such as Kitty-Cache you can see that you could replace the Map with a Multimap and rewrite the other methods accordingly. So in KittyCache.java internally you could have something like:

Multimap<K, CacheEntry<V>> cache;

The trick for this kind of cache is that nothing really expires until someone requests it.

查看更多
Explosion°爆炸
4楼-- · 2019-04-04 09:38

I think that Louis Wasserman provided the answer in one of the comments above, i.e. that there is no off-the-shelf combo of Multimap and Cache available. I have solved my problem/requirements with the solution outlined in pseudo-code below:

private Cache<Integer,Object> cache = CacheBuilder.newBuilder().SomeConfig.build();
private Multimap<Integer,Object> multimap = HashMultimap<Integer, Object>.create();
private AtomicInteger atomicid = new AtomicInteger(0);

public void putInMultimap(int id, Object obj) {
   int mapid = atomicid.addAndGet(1);
   cache.put(mapid,obj);
   multimap.put(id,mapid);
}
public List<Object> getFromMultimap(int id) {
   Set<Integer> mapids = multimap.get(id);
   List<Object> list = new ArrayList<Object>();
   for (int i : mapids) {
      list.add(cache.getIfPresent(i));
   }
   return list;
}

This simple 'solution' has some limitations but it works OK for me.

查看更多
登录 后发表回答