1

I am trying to build a cache using https://github.com/ben-manes/caffeine, where I need to fetch all entries during boot up time and I do not know all the keys before hand. My CachLoader has something like this and trying to cache all at startup. But looks like I will need to know all the keys before hand, if I want to prefetch all entries in to cache? Am I missing anything?

So, say I call cache.getAll(10) and only 10-->100 will be cached even though the loadAll method returns 3 entries (10-->100, 20-->400, 30-->900)

@Override
public Map<Integer, Integer> loadAll(Iterable<? extends Integer> keys)
{
    // gets called for the keys which are not present in the cache only by getAll
    // we will load all keys irrespective of what is passed in
    System.out.println("in loadAll");
    // problem here is it is only caching the supplied keys
    Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    map.put(10, 100);
    map.put(20, 400);
    map.put(30, 900);
    return map;
}   
user3310917
  • 405
  • 5
  • 13
  • What is your specific question? – Russbear May 30 '16 at 23:12
  • my question is do I need to know all possible keys before hand so that i can prefetch all possible entries? – user3310917 May 30 '16 at 23:14
  • 4
    `loadAll` is [allowed](https://github.com/ben-manes/caffeine/wiki/Population#loading) to return a larger mapping than the keys requested and all will be cached (but only the requested entries returned). If you want to load everything then you might not have a key-value cache and Guava's `Suppliers.memoizeWithExpiration` could be a better fit. – Ben Manes May 30 '16 at 23:46
  • Thanks a lot for the reply Ben. looks like i do not need to know all possible keys. I can pass in a fake key and load them all in loadAll() – user3310917 May 31 '16 at 00:57

0 Answers0