3
votes

I am using Guava LoadingCache to bulk load all elements at once into my eager cache. But the implementation of the loadAll method that I'm supplying does not really need an Iterable<? extends K> keys argument, since my DAO does not except any parameters either - my DAO method returns generic Map<K,V>.

Since my implementation is generic, I'm using generics to do a call on getAllIterable(<? extends K> keys_), but because of the type erasure, I can not instantiate K key, and pass it to getAll, since it does not expect any non null keys.

Does anyone know of any workaround around this?

1
Still trying to understand what you're doing. Do you just have a method which provides a Map of everything you ought to have in your cache to start with, without querying any specific keys? Or are you trying to get out all values from your cache without querying a specific key? - Louis Wasserman
There is a conflict of interests here; you said you want to load all entries from the cache; what is "all"? You make it sound like this "all" is infinite - fge
@LouisWasserman That's correct. I bulk load a map<K,V> without any specific keys - GMoney
But you can't do that! You have to know the keys beforehand. The fact that you can .get() any key on demand is what a LoadingCache is for - fge
@fge - I never said I bulk load anything from the cache - I need to do a bulk load a bunch of Key/Values into LoadingCache by implementing CacheLoader<K,V> loadAll method, which is triggered whent getall method is invoked on your instance of LoadingCache class - GMoney

1 Answers

4
votes

If the goal is just to prepopulate a Cache with the contents of a Map<K, V>, then you should just use Cache.putAll(Map<K, V>) to put all the entries from a specified Map in the cache.