Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guava Load Multiple Keys and Get multiple items

Tags:

java

guava

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?

like image 397
user3019766 Avatar asked Jun 07 '26 15:06

user3019766


1 Answers

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);
like image 146
shmosel Avatar answered Jun 09 '26 04:06

shmosel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!