I would like to use Guava as cache but I can't seem to find Guava has the capability of allowing me to load multiple items and get multiple items.
I see CacheLoader has the following:
@Override
public Value load(String key) {
return getKey();
}
And what I need to load is:
@Override
public List<Value> load(List<String> keys) {
return getKeys();
}
I would also expect to get one or a list of items from the cache, but I am happy even if I had to wrap that one item into a list just to get it.
I'm new to Guava and I'm not sure if Guava has such functionality?
You can use CacheLoader.loadAll() to load multiple items, and LoadingCache.getAll() to get them.
For example:
new CacheLoader<String, Value>() {
@Override
public Value load(String key) {
return getKey();
}
@Override
public Map<String, Value> load(Iterable<? extends String> keys) {
return getKeys();
}
}
//...
List<String> keys = Arrays.asList("key1", "key2", "key3");
ImmutableMap<String, Value> values = cache.getAll(keys);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With