I'm trying to use google guava cache on a program but am not quite getting how it works.
I'm loading up the cache and then at a later stage i'm trying to check if an item exists in the cache, my code below doesnt quite work
The getIfPresent returns null if it doesnt exist but the load which calls it bombs out after with the error
Exception in thread "main" com.google.common.cache.CacheLoader$InvalidCacheLoadException: CacheLoader returned null for key
private static LoadingCache<String, Image> imageCache
= CacheBuilder.newBuilder()
.build(new CacheLoader<String, Image>() {
@Override
public Image load(String key) throws Exception {
if (getImage(key) != null) {
return getImage(key);
}
return null;
}
});
public static Image getImage(String key) throws ExecutionException {
return imageCache.getIfPresent(key);
}
this means i cant check for the presense of the item in the cache like so
try {
readImage = imageCache.get(fileName);
} catch (ExecutionException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (readImage != null) {
}
can someone explain to me what i'm doing wrong here?