title: How to cache server results in GWT with guava?
body: <p>In my GWT Application I'm often refering several times to the same server results. I also don't know which code is executed first. I therefore want to use caching of my asynchronous (client-side) results.</p>
  
  <p>I want to use an existing caching library; I'm considering <strong>guava-gwt</strong>.</p>
  
  <p>I found this example of a Guava <strong>synchronous</strong> cache (in <a href="https://code.google.com/p/guava-libraries/wiki/CachesExplained" rel="nofollow">guava's documentation</a>):</p>
  
  <pre><code>LoadingCache&lt;Key, Graph&gt; graphs = CacheBuilder.newBuilder()
         .build(
             new CacheLoader&lt;Key, Graph&gt;() {
               public Graph load(Key key) throws AnyException {
                 return createExpensiveGraph(key);
               }
             });
  </code></pre>
  
  <p>This is how I'm trying to use a Guava cache <strong>asynchronously</strong> (I have no clue about how to make this work):</p>
  
  <pre><code>LoadingCache&lt;Key, Graph&gt; graphs = CacheBuilder.newBuilder()
         .build(
             new CacheLoader&lt;Key, Graph&gt;() {
               public Graph load(Key key) throws AnyException {
  
                 // I want to do something asynchronous here, I cannot use Thread.sleep in the browser/JavaScript environment.
                 service.createExpensiveGraph(key, new AsyncCallback&lt;Graph&gt;() {
  
                   public void onFailure(Throwable caught) {
                     // how to tell the cache about the failure???
                   }
  
                   public void onSuccess(Graph result) {
                     // how to fill the cache with that result???
                   }
                 });
  
                 return // I cannot provide any result yet. What can I return???
               }
             });
  </code></pre>
  
  <p>GWT is missing many classes from the default JRE (especially concerning threads and concurrancy).</p>
  
  <p>How can I use guava-gwt to cache asynchronous results?</p>