報錯
loadAll failed to return a value for
try {
if (!keysToLoad.isEmpty()) {
try {
Map<K, V> newEntries = loadAll(keysToLoad, defaultLoader);
for (K key : keysToLoad) {
V value = newEntries.get(key);
if (value == null) {
throw new InvalidCacheLoadException("loadAll failed to return a value for " + key);
}
result.put(key, value);
}
} catch (UnsupportedLoadingOperationException e) {
// loadAll not implemented, fallback to load
for (K key : keysToLoad) {
misses--; // get will count this miss
result.put(key, get(key, defaultLoader));
}
}
}
return ImmutableMap.copyOf(result);
}
這段代碼意味着,所有的key必須要有value,如果沒有緩存到,那麼必然報錯
解決
1、可以通過緩存value不存在的,指定一個特定的值作爲所謂的“空值”,這種情況適用於,大面積key都可以覆蓋到,只有極少數
2、可以在上層直接catch這個異常,然後什麼都不做,這種情況適合很多key都找不到value的情況