Add cache for name lookup

This commit is contained in:
tastybento 2024-07-05 10:19:21 -07:00
parent f4604b4c27
commit cb2d0e7efd
2 changed files with 71 additions and 7 deletions

View File

@ -3,6 +3,7 @@ package world.bentobox.bentobox.managers;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
@ -30,7 +31,7 @@ public class PlayersManager {
private Database<Players> handler; private Database<Players> handler;
private final Database<Names> names; private final Database<Names> names;
private final Map<UUID, Players> playerCache = new ConcurrentHashMap<>(); private final Map<UUID, Players> playerCache = new ConcurrentHashMap<>();
private final @NonNull List<Names> nameCache;
private final Set<UUID> inTeleport; // this needs databasing private final Set<UUID> inTeleport; // this needs databasing
/** /**
@ -46,6 +47,7 @@ public class PlayersManager {
handler = new Database<>(plugin, Players.class); handler = new Database<>(plugin, Players.class);
// Set up the names database // Set up the names database
names = new Database<>(plugin, Names.class); names = new Database<>(plugin, Names.class);
nameCache = names.loadObjects();
inTeleport = new HashSet<>(); inTeleport = new HashSet<>();
} }
@ -139,7 +141,7 @@ public class PlayersManager {
// Not used // Not used
} }
} }
return names.loadObjects().stream().filter(n -> n.getUniqueId().equalsIgnoreCase(name)).findFirst() return nameCache.stream().filter(n -> n.getUniqueId().equalsIgnoreCase(name)).findFirst()
.map(Names::getUuid).orElse(null); .map(Names::getUuid).orElse(null);
} }
@ -148,10 +150,18 @@ public class PlayersManager {
* @param user - the User * @param user - the User
*/ */
public void setPlayerName(@NonNull User user) { public void setPlayerName(@NonNull User user) {
// Ignore any bots
if (user.getUniqueId() == null) {
return;
}
Players player = getPlayer(user.getUniqueId()); Players player = getPlayer(user.getUniqueId());
player.setPlayerName(user.getName()); player.setPlayerName(user.getName());
handler.saveObject(player); handler.saveObject(player);
// Update names
Names newName = new Names(user.getName(), user.getUniqueId()); Names newName = new Names(user.getName(), user.getUniqueId());
// Add to cache
nameCache.removeIf(name -> user.getUniqueId().equals(name.getUuid()));
nameCache.add(newName);
// Add to names database // Add to names database
names.saveObjectAsync(newName); names.saveObjectAsync(newName);
} }

View File

@ -9,6 +9,7 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@ -71,7 +72,7 @@ import world.bentobox.bentobox.util.Util;
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class, Logger.class, DatabaseSetup.class, }) @PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class, Logger.class, DatabaseSetup.class })
public class PlayersManagerTest { public class PlayersManagerTest {
private static AbstractDatabaseHandler<Object> handler; private static AbstractDatabaseHandler<Object> handler;
@ -685,12 +686,65 @@ public class PlayersManagerTest {
} }
/** /**
* Test method for * Test method for {@link world.bentobox.bentobox.managers.PlayersManager#getPlayer(java.util.UUID)}.
* {@link world.bentobox.bentobox.managers.PlayersManager#shutdown()}.
*/ */
@Test @Test
public void testShutdown() { public void testGetPlayer() {
pm.shutdown(); // Clears cache Players p = pm.getPlayer(uuid);
assertNotNull(p);
}
/**
* Test method for {@link world.bentobox.bentobox.managers.PlayersManager#loadPlayer(java.util.UUID)}.
*/
@Test
public void testLoadPlayer() {
assertNotNull(pm.loadPlayer(uuid));
}
/**
* Test method for {@link world.bentobox.bentobox.managers.PlayersManager#getName(java.util.UUID)}.
*/
@Test
public void testGetName() {
assertEquals("", pm.getName(uuid));
}
/**
* Test method for {@link world.bentobox.bentobox.managers.PlayersManager#cleanLeavingPlayer(org.bukkit.World, world.bentobox.bentobox.api.user.User, boolean, world.bentobox.bentobox.database.objects.Island)}.
*/
@Test
public void testCleanLeavingPlayer() {
when(user.isOnline()).thenReturn(true);
when(iwm.isOnLeaveResetEnderChest(world)).thenReturn(true);
when(iwm.isOnLeaveResetInventory(world)).thenReturn(true);
when(iwm.isOnLeaveResetMoney(world)).thenReturn(true);
pm.cleanLeavingPlayer(world, user, false, island);
PowerMockito.verifyStatic(Util.class);
Util.runCommands(user, "", plugin.getIWM().getOnLeaveCommands(world), "leave");
verify(world).getEntitiesByClass(Tameable.class);
verify(inv).clear(); // Enderchest cleared
verify(plugin).getVault(); // Clear money
PowerMockito.verifyStatic(Util.class);
Util.resetHealth(p);
verify(p).setFoodLevel(20);
verify(p).setLevel(0);
verify(p).setExp(0);
// Player total XP (not displayed)
verify(p).setTotalExperience(0);
}
/**
* Test method for {@link world.bentobox.bentobox.managers.PlayersManager#savePlayer(java.util.UUID)}.
* @throws IntrospectionException
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
@Test
public void testSavePlayer() throws IllegalAccessException, InvocationTargetException, IntrospectionException {
pm.savePlayer(uuid);
verify(handler, atLeastOnce()).saveObject(any());
} }
} }