Add a cache for prisoners online only, this should help performance #26

The cache listens to a lot of events and custom events to ensure the
cache is up to date, this way we don't have to loop through all the
prisoners in a jail every single time they move or something but instead
just check the cache.
This commit is contained in:
graywolf336 2014-05-30 12:23:32 -05:00
parent 82f17f3a4b
commit af1fa37470
5 changed files with 144 additions and 3 deletions

View File

@ -12,6 +12,7 @@ import com.graywolf336.jail.command.JailHandler;
import com.graywolf336.jail.enums.Settings; import com.graywolf336.jail.enums.Settings;
import com.graywolf336.jail.legacy.LegacyManager; import com.graywolf336.jail.legacy.LegacyManager;
import com.graywolf336.jail.listeners.BlockListener; import com.graywolf336.jail.listeners.BlockListener;
import com.graywolf336.jail.listeners.CacheListener;
import com.graywolf336.jail.listeners.EntityListener; import com.graywolf336.jail.listeners.EntityListener;
import com.graywolf336.jail.listeners.HandCuffListener; import com.graywolf336.jail.listeners.HandCuffListener;
import com.graywolf336.jail.listeners.JailingListener; import com.graywolf336.jail.listeners.JailingListener;
@ -79,6 +80,7 @@ public class JailMain extends JavaPlugin {
PluginManager plm = this.getServer().getPluginManager(); PluginManager plm = this.getServer().getPluginManager();
plm.registerEvents(new BlockListener(this), this); plm.registerEvents(new BlockListener(this), this);
plm.registerEvents(new CacheListener(this), this);
plm.registerEvents(new EntityListener(this), this); plm.registerEvents(new EntityListener(this), this);
plm.registerEvents(new HandCuffListener(this), this); plm.registerEvents(new HandCuffListener(this), this);
plm.registerEvents(new JailingListener(this), this); plm.registerEvents(new JailingListener(this), this);

View File

@ -9,6 +9,7 @@ import org.bukkit.Location;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.graywolf336.jail.beans.CachePrisoner;
import com.graywolf336.jail.beans.Cell; import com.graywolf336.jail.beans.Cell;
import com.graywolf336.jail.beans.ConfirmPlayer; import com.graywolf336.jail.beans.ConfirmPlayer;
import com.graywolf336.jail.beans.CreationPlayer; import com.graywolf336.jail.beans.CreationPlayer;
@ -42,6 +43,7 @@ public class JailManager {
private HashMap<String, CreationPlayer> jailCreators; private HashMap<String, CreationPlayer> jailCreators;
private HashMap<String, CreationPlayer> cellCreators; private HashMap<String, CreationPlayer> cellCreators;
private HashMap<String, ConfirmPlayer> confirms; private HashMap<String, ConfirmPlayer> confirms;
private HashMap<UUID, CachePrisoner> cache;
private JailCreationSteps jcs; private JailCreationSteps jcs;
private CellCreationSteps ccs; private CellCreationSteps ccs;
@ -51,6 +53,7 @@ public class JailManager {
this.jailCreators = new HashMap<String, CreationPlayer>(); this.jailCreators = new HashMap<String, CreationPlayer>();
this.cellCreators = new HashMap<String, CreationPlayer>(); this.cellCreators = new HashMap<String, CreationPlayer>();
this.confirms = new HashMap<String, ConfirmPlayer>(); this.confirms = new HashMap<String, ConfirmPlayer>();
this.cache = new HashMap<UUID, CachePrisoner>();
this.jcs = new JailCreationSteps(); this.jcs = new JailCreationSteps();
this.ccs = new CellCreationSteps(); this.ccs = new CellCreationSteps();
} }
@ -169,6 +172,36 @@ public class JailManager {
return cells; return cells;
} }
/**
* Adds a prisoner to the cache.
*
* @param cache object to store
* @return The same object given
*/
public CachePrisoner addCacheObject(CachePrisoner cache) {
this.cache.put(cache.getPrisoner().getUUID(), cache);
return cache;
}
/**
* Checks if the given uuid is in the cache.
*
* @param uuid of the player
* @return true if in cache, false if not
*/
public boolean inCache(UUID uuid) {
return this.cache.containsKey(uuid);
}
/**
* Removes the cache object stored for this uuid.
*
* @param uuid of the prisoner to remove
*/
public void removeCacheObject(UUID uuid) {
this.cache.remove(uuid);
}
/** /**
* Gets all the prisoners in the system, best for a system wide count of the prisoners or accessing all the prisoners at once. * Gets all the prisoners in the system, best for a system wide count of the prisoners or accessing all the prisoners at once.
* *
@ -191,17 +224,22 @@ public class JailManager {
*/ */
public Jail getJailPrisonerIsIn(Prisoner prisoner) { public Jail getJailPrisonerIsIn(Prisoner prisoner) {
if(prisoner == null) return null; if(prisoner == null) return null;
else return getJailPlayerIsIn(prisoner.getUUID());
return getJailPlayerIsIn(prisoner.getUUID());
} }
/** /**
* Gets the {@link Jail jail} the given player is in. * Gets the {@link Jail jail} the given player is in.
* *
* <p />
*
* Checks the cache first.
*
* @param uuid The uuid of the player who's jail we are getting. * @param uuid The uuid of the player who's jail we are getting.
* @return The jail the player is in, <strong>CAN BE NULL</strong>. * @return The jail the player is in, <strong>CAN BE NULL</strong>.
*/ */
public Jail getJailPlayerIsIn(UUID uuid) { public Jail getJailPlayerIsIn(UUID uuid) {
if(this.cache.containsKey(uuid)) return this.cache.get(uuid).getJail();
for(Jail j : jails.values()) for(Jail j : jails.values())
if(j.isPlayerJailed(uuid)) if(j.isPlayerJailed(uuid))
return j; return j;

View File

@ -0,0 +1,28 @@
package com.graywolf336.jail.beans;
/**
* An object for storing online cached prisoners.
*
* @author graywolf336
* @since 3.0.0
* @version 1.0.0
*/
public class CachePrisoner {
private Jail jail;
private Prisoner p;
public CachePrisoner(Jail jail, Prisoner prisoner) {
this.jail = jail;
this.p = prisoner;
}
/** Gets the Jail this cache is in. */
public Jail getJail() {
return this.jail;
}
/** Gets the Prisoner in this cache. */
public Prisoner getPrisoner() {
return this.p;
}
}

View File

@ -0,0 +1,73 @@
package com.graywolf336.jail.listeners;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import com.graywolf336.jail.JailMain;
import com.graywolf336.jail.beans.CachePrisoner;
import com.graywolf336.jail.beans.Jail;
import com.graywolf336.jail.beans.Prisoner;
import com.graywolf336.jail.events.PrePrisonerTransferredEvent;
import com.graywolf336.jail.events.PrisonerJailedEvent;
import com.graywolf336.jail.events.PrisonerReleasedEvent;
import com.graywolf336.jail.events.PrisonerTransferredEvent;
public class CacheListener implements Listener {
private JailMain pl;
public CacheListener(JailMain plugin) {
this.pl = plugin;
}
@EventHandler(priority = EventPriority.LOWEST)
public void joinListener(PlayerJoinEvent event) {
if(pl.getJailManager().isPlayerJailed(event.getPlayer().getUniqueId())) {
Jail j = pl.getJailManager().getJailPlayerIsIn(event.getPlayer().getUniqueId());
Prisoner p = j.getPrisoner(event.getPlayer().getUniqueId());
pl.getJailManager().addCacheObject(new CachePrisoner(j, p));
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void leaveListener(PlayerQuitEvent event) {
if(pl.getJailManager().inCache(event.getPlayer().getUniqueId())) {
pl.getJailManager().removeCacheObject(event.getPlayer().getUniqueId());
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void kickListener(PlayerKickEvent event) {
if(pl.getJailManager().inCache(event.getPlayer().getUniqueId())) {
pl.getJailManager().removeCacheObject(event.getPlayer().getUniqueId());
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void jailListener(PrisonerJailedEvent event) {
pl.getJailManager().addCacheObject(new CachePrisoner(event.getJail(), event.getPrisoner()));
}
@EventHandler(priority = EventPriority.LOWEST)
public void releaseListener(PrisonerReleasedEvent event) {
if(pl.getJailManager().inCache(event.getPlayer().getUniqueId())) {
pl.getJailManager().removeCacheObject(event.getPlayer().getUniqueId());
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void beforeTransferringListener(PrePrisonerTransferredEvent event) {
if(pl.getJailManager().inCache(event.getPlayer().getUniqueId())) {
pl.getJailManager().removeCacheObject(event.getPlayer().getUniqueId());
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void transferListener(PrisonerTransferredEvent event) {
pl.getJailManager().addCacheObject(new CachePrisoner(event.getTargetJail(), event.getPrisoner()));
}
}

View File

@ -85,7 +85,7 @@ public class PlayerListener implements Listener {
} }
} }
@EventHandler @EventHandler(priority = EventPriority.HIGHEST)
public void checkForOfflineJailStuff(PlayerJoinEvent event) { public void checkForOfflineJailStuff(PlayerJoinEvent event) {
//Let's check if the player is jailed //Let's check if the player is jailed
if(pl.getJailManager().isPlayerJailed(event.getPlayer().getUniqueId())) { if(pl.getJailManager().isPlayerJailed(event.getPlayer().getUniqueId())) {