Improve cache management.

Do not load players in memory, and unload them when they leave.
This commit is contained in:
BONNe 2019-04-27 00:13:02 +03:00
parent ae384c433d
commit 0418e64125
2 changed files with 61 additions and 6 deletions

View File

@ -133,7 +133,8 @@ public class ChallengesManager
this.challengeDatabase.loadObjects().forEach(this::loadChallenge);
this.levelDatabase.loadObjects().forEach(this::loadLevel);
this.playersDatabase.loadObjects().forEach(this::loadPlayerData);
// It is not necessary to load all players in memory.
// this.playersDatabase.loadObjects().forEach(this::loadPlayerData);
}
@ -150,7 +151,8 @@ public class ChallengesManager
this.challengeDatabase.loadObjects().forEach(this::loadChallenge);
this.levelDatabase.loadObjects().forEach(this::loadLevel);
this.playersDatabase.loadObjects().forEach(this::loadPlayerData);
// It is not necessary to load all players in memory.
// this.playersDatabase.loadObjects().forEach(this::loadPlayerData);
}
@ -304,6 +306,28 @@ public class ChallengesManager
}
/**
* This method removes given player from cache data.
* @param playerID player ID which cache data must be removed.
*/
public void removeFromCache(UUID playerID)
{
if (!this.settings.isStoreAsIslandData())
{
if (this.playerCacheData.containsKey(playerID.toString()))
{
// save before remove
this.savePlayerData(playerID.toString());
this.playerCacheData.remove(playerID.toString());
}
}
// TODO: It would be necessary to remove also data, if they stores islands.
// Unfortunately, I do not know all worlds. Checking everything would be bad. Probably, I could
// add extra map that links players with their cached island data?
}
// ---------------------------------------------------------------------
// Section: Other storing related methods
// ---------------------------------------------------------------------
@ -356,17 +380,17 @@ public class ChallengesManager
// The player is not in the cache
// Check if the player exists in the database
if (this.playersDatabase.objectExists(uniqueID.toString()))
if (this.playersDatabase.objectExists(uniqueID))
{
// Load player from database
ChallengesPlayerData data = this.playersDatabase.loadObject(uniqueID.toString());
ChallengesPlayerData data = this.playersDatabase.loadObject(uniqueID);
// Store in cache
this.playerCacheData.put(uniqueID, data);
}
else
{
// Create the player data
ChallengesPlayerData pd = new ChallengesPlayerData(uniqueID.toString());
ChallengesPlayerData pd = new ChallengesPlayerData(uniqueID);
this.playersDatabase.saveObject(pd);
// Add to cache
this.playerCacheData.put(uniqueID, pd);

View File

@ -4,6 +4,8 @@ package world.bentobox.challenges.listeners;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.world.WorldSaveEvent;
import world.bentobox.challenges.ChallengesAddon;
@ -20,16 +22,45 @@ public class SaveListener implements Listener
}
/**
* This event listener handles world save event.
* @param e World Save event.
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onWorldSave(WorldSaveEvent e)
{
if (!this.addon.getChallengesManager().getAllChallenges(e.getWorld()).isEmpty())
// Save only for worlds where exist any challenge addon data.
if (this.addon.getChallengesManager().hasAnyChallengeData(e.getWorld()))
{
this.addon.getChallengesManager().save();
}
}
/**
* This event listener handles player kick event.
* If player is kicked, then remove it from player cache data.
* @param e PlayerKickEvent
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerKickEvent(PlayerKickEvent e)
{
this.addon.getChallengesManager().removeFromCache(e.getPlayer().getUniqueId());
}
/**
* This event listener handles player quit event.
* If player quits server, then remove it from player cache data.
* @param e PlayerQuitEvent
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerQuitEvent(PlayerQuitEvent e)
{
this.addon.getChallengesManager().removeFromCache(e.getPlayer().getUniqueId());
}
// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------