Sync old changes

This commit is contained in:
Oleg Agafonov 2019-03-22 21:51:26 +04:00
parent caf1a98759
commit 270ec615af
4 changed files with 35 additions and 17 deletions

View file

@ -9,10 +9,12 @@ import com.google.common.base.Function;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.ForwardingLoadingCache;
import com.google.common.cache.LoadingCache;
import org.apache.log4j.Logger;
public class SoftValuesLoadingCache<K, V> extends ForwardingLoadingCache<K, Optional<V>> {
private final LoadingCache<K, Optional<V>> cache;
private static final Logger logger = Logger.getLogger(SoftValuesLoadingCache.class);
public SoftValuesLoadingCache(CacheLoader<K, Optional<V>> loader) {
cache = newBuilder().softValues().build(loader);
@ -35,7 +37,14 @@ public class SoftValuesLoadingCache<K, V> extends ForwardingLoadingCache<K, Opti
try {
return get(key).orElse(null);
} catch (ExecutionException e) {
throw new RuntimeException(e);
if (e.getCause() instanceof OutOfMemoryError) {
logger.warn("Out of memory error: try to increase free memory in launcher options (-xmx param)");
return null;
} else {
throw new RuntimeException(e);
}
} catch (Throwable e) {
return null;
}
}