Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ public interface ISettings extends IConf {

int getMaxUserCacheCount();

long getMaxUserCacheValueExpiry();

boolean allowSilentJoinQuit();

boolean isCustomJoinMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1469,10 +1469,16 @@ public boolean isDropItemsIfFull() {
// #easteregg
@Override
public int getMaxUserCacheCount() {
final long count = Runtime.getRuntime().maxMemory() / 1024 / 96;
final long count = Runtime.getRuntime().maxMemory() / 1024 / 1024;
return config.getInt("max-user-cache-count", (int) count);
}

// #easteregg
@Override
public long getMaxUserCacheValueExpiry() {
return config.getLong("max-user-cache-value-expiry", 600);
}

@Override
public boolean isLastMessageReplyRecipient() {
return config.getBoolean("last-message-reply-recipient", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;

Expand All @@ -26,23 +27,28 @@ public class ModernUserMap extends CacheLoader<UUID, User> implements IUserMap {

private final boolean debugPrintStackWithWarn;
private final long debugMaxWarnsPerType;
private final boolean debugLogCache;
private final ConcurrentMap<String, AtomicLong> debugNonPlayerWarnCounts;

public ModernUserMap(final IEssentials ess) {
this.ess = ess;
this.uuidCache = new ModernUUIDCache(ess);
this.userCache = CacheBuilder.newBuilder()
.maximumSize(ess.getSettings().getMaxUserCacheCount())
.expireAfterAccess(ess.getSettings().getMaxUserCacheValueExpiry(), TimeUnit.SECONDS)
.softValues()
.build(this);

// -Dnet.essentialsx.usermap.print-stack=true
final String printStackProperty = System.getProperty("net.essentialsx.usermap.print-stack", "false");
// -Dnet.essentialsx.usermap.max-warns=20
final String maxWarnProperty = System.getProperty("net.essentialsx.usermap.max-warns", "100");
// -Dnet.essentialsx.usermap.log-cache=true
final String logCacheProperty = System.getProperty("net.essentialsx.usermap.log-cache", "false");

this.debugMaxWarnsPerType = NumberUtil.isLong(maxWarnProperty) ? Long.parseLong(maxWarnProperty) : -1;
this.debugPrintStackWithWarn = Boolean.parseBoolean(printStackProperty);
this.debugLogCache = Boolean.parseBoolean(logCacheProperty);
this.debugNonPlayerWarnCounts = new ConcurrentHashMap<>();
}

Expand Down Expand Up @@ -81,6 +87,7 @@ public User getUser(final UUID uuid) {
public User getUser(final Player base) {
final User user = loadUncachedUser(base);
userCache.put(user.getUUID(), user);
debugLogCache(user);
return user;
}

Expand Down Expand Up @@ -114,6 +121,7 @@ public void addCachedNpcName(final UUID uuid, final String name) {
public User load(final UUID uuid) throws Exception {
final User user = loadUncachedUser(uuid);
if (user != null) {
debugLogCache(user);
return user;
}

Expand Down Expand Up @@ -168,6 +176,7 @@ public User loadUncachedUser(final UUID uuid) {

public void addCachedUser(final User user) {
userCache.put(user.getUUID(), user);
debugLogCache(user);
}

@Override
Expand Down Expand Up @@ -196,6 +205,14 @@ public void shutdown() {
uuidCache.shutdown();
}

private void debugLogCache(final User user) {
if (!debugLogCache) {
return;
}
final Throwable throwable = new Throwable();
ess.getLogger().log(Level.INFO, String.format("Caching user %s (%s)", user.getName(), user.getUUID()), throwable);
}

private void debugLogUncachedNonPlayer(final Player base) {
final String typeName = base.getClass().getName();
final long count = debugNonPlayerWarnCounts.computeIfAbsent(typeName, name -> new AtomicLong(0)).getAndIncrement();
Expand Down