Created
November 7, 2014 13:08
-
-
Save ericcitaire/62fcad871f2638341db5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LoadingCache<String, Future<String>> cache = CacheBuilder.newBuilder() | |
.build( | |
new CacheLoader<String, Future<String>>() { | |
private int counter = 0; | |
@Override | |
public Future<String> load(final String key) { | |
++counter; | |
if (counter == 1) { | |
System.out.println("premier load"); | |
// le premier load lève une exeption | |
return Futures.immediateFailedFuture(new IllegalStateException("Oups!")); | |
} else { | |
System.out.println("deuxième load"); | |
// le deuxième load se passe bien | |
return Futures.immediateFuture("bar"); | |
} | |
} | |
}); | |
try { | |
System.out.println("premier get"); | |
String boo = cache.get("foo").get(); | |
System.out.println("foo = " + boo); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
try { | |
System.out.println("deuxième get"); | |
String boo = cache.get("foo").get(); | |
System.out.println("foo = " + boo); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment