我尝试使用MapMaker / CacheBuilder进行缓存,但我不明白如何正确处理空值。
ConcurrentMap graphs = new MapMaker()
.concurrencyLevel(4)
.weakKeys()
.maximumSize(10000)
.expireAfterWrite(10, TimeUnit.MINUTES)
.makeComputingMap(
new Function() {
public Graph apply(Key key) {
return createExpensiveGraph(key);
}
});
如果方法createExpensiveGraph返回一个空值,那么抛出NullpointerException。
我不明白为什么ComputingConcurrentHashMap引发一个NPE,而不是只返回一个null值。
如何正确处理这个?只是捕获NPE并返回null?
我缺少什么?