Skip to content

Instantly share code, notes, and snippets.

@ericcitaire
Created November 7, 2014 13:08
Show Gist options
  • Save ericcitaire/62fcad871f2638341db5 to your computer and use it in GitHub Desktop.
Save ericcitaire/62fcad871f2638341db5 to your computer and use it in GitHub Desktop.
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