Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does someone use Guava's CacheLoader asynchronously

Tags:

caching

guava

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.

like image 468
Guillaume Pelletier Avatar asked Oct 25 '11 19:10

Guillaume Pelletier


2 Answers

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

like image 95
fry Avatar answered Oct 19 '22 23:10

fry


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 */
         }
       });
like image 23
harningt Avatar answered Oct 20 '22 00:10

harningt