Guillaume Pelletier
Guillaume Pelletier

Reputation: 121

How does someone use Guava's CacheLoader asynchronously

The question says it all I'd like to use CacheBuilder, but my values are pulled in asynchronously. This worked previously with MapMaker as the CacheLoader wasn't a requirement. Now I'd like to know if I can hack this up or if there are any non deprecated alternatives. Thank you.

Upvotes: 5

Views: 2660

Answers (2)

harningt
harningt

Reputation: 709

One method would be to make with generic parameters K and V as your desired outputs:

LoadingCache<K, ListenableFuture<V>> values = CacheBuilder.newBuilder()
   .build(
       new CacheLoader<K, ListenableFuture<V>>() {
         public ListenableFuture<V> load(K key) {
           /* Get your future */
         }
       });

Upvotes: 1

fry
fry

Reputation: 591

I think the question you're trying to ask is "How can I use CacheBuilder without having to specify a CacheLoader?" If that's the case, then there will be support for this in Guava release 11.0. In the meantime a build() method on CacheLoader is already checked into trunk (as of this morning):

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/cache/CacheBuilder.html

Upvotes: 5

Related Questions