Skip to content

Commit

Permalink
Improve cache management.
Browse files Browse the repository at this point in the history
Do not load players in memory, and unload them when they leave.
  • Loading branch information
BONNe committed Apr 26, 2019
1 parent ae384c4 commit 0418e64
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
34 changes: 29 additions & 5 deletions src/main/java/world/bentobox/challenges/ChallengesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public void load()

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);
}


Expand All @@ -150,7 +151,8 @@ public void reload()

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);
}


Expand Down Expand Up @@ -304,6 +306,28 @@ private void loadPlayerData(ChallengesPlayerData playerData)
}


/**
* 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
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -356,17 +380,17 @@ private void addPlayerData(@NonNull String uniqueID)
// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
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;
Expand All @@ -20,16 +22,45 @@ public SaveListener(ChallengesAddon addon) {
}


/**
* 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
// ---------------------------------------------------------------------
Expand Down

0 comments on commit 0418e64

Please sign in to comment.