Remove null cache values

https://github.com/BentoBoxWorld/Level/issues/126
This commit is contained in:
tastybento 2020-02-01 20:24:08 -08:00
parent b3b6502ef3
commit 40681190c1
1 changed files with 11 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package world.bentobox.level;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -81,7 +82,12 @@ public class Level extends Addon {
public LevelsData getLevelsData(@NonNull UUID targetPlayer) { public LevelsData getLevelsData(@NonNull UUID targetPlayer) {
// Get from database if not in cache // Get from database if not in cache
if (!levelsCache.containsKey(targetPlayer) && handler.objectExists(targetPlayer.toString())) { if (!levelsCache.containsKey(targetPlayer) && handler.objectExists(targetPlayer.toString())) {
levelsCache.put(targetPlayer, handler.loadObject(targetPlayer.toString())); LevelsData ld = handler.loadObject(targetPlayer.toString());
if (ld != null) {
levelsCache.put(targetPlayer, ld);
} else {
handler.deleteID(targetPlayer.toString());
}
} }
// Return cached value or null // Return cached value or null
return levelsCache.get(targetPlayer); return levelsCache.get(targetPlayer);
@ -215,7 +221,8 @@ public class Level extends Addon {
* Save the levels to the database * Save the levels to the database
*/ */
private void save(){ private void save(){
// No async for now // Remove any potential null values from the cache
levelsCache.values().removeIf(Objects::isNull);
levelsCache.values().forEach(handler::saveObject); levelsCache.values().forEach(handler::saveObject);
} }
@ -262,6 +269,8 @@ public class Level extends Addon {
* @return level or 0 by default * @return level or 0 by default
*/ */
public long getInitialIslandLevel(@NonNull Island island) { public long getInitialIslandLevel(@NonNull Island island) {
// Remove any potential null values from the cache
levelsCache.values().removeIf(Objects::isNull);
return levelsCache.containsKey(island.getOwner()) ? levelsCache.get(island.getOwner()).getInitialLevel(island.getWorld()) : 0L; return levelsCache.containsKey(island.getOwner()) ? levelsCache.get(island.getOwner()).getInitialLevel(island.getWorld()) : 0L;
} }