Skip to content

Commit

Permalink
PlayerFlagHandler: clean secondary cache less often
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Feb 25, 2021
1 parent 1881396 commit cdf9c9d
Showing 1 changed file with 13 additions and 6 deletions.
Expand Up @@ -4,15 +4,13 @@
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
import com.denizenscript.denizencore.flags.SavableMapFlagTracker;
import com.denizenscript.denizencore.scripts.ScriptHelper;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.scheduler.BukkitRunnable;

import java.io.File;
import java.io.FileInputStream;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -57,19 +55,28 @@ public boolean shouldExpire() {

private static ArrayList<UUID> toClearCache = new ArrayList<>();

public static void cleanCache() {
if (cacheTimeoutSeconds == -1) {
return;
}
public static void cleanSecondaryCache() {
toClearCache.clear();
for (Map.Entry<UUID, SoftReference<CachedPlayerFlag>> entry : secondaryPlayerFlagTrackerCache.entrySet()) {
// NOTE: This call will make the GC think the value is still needed, thus the 10 minute cleanup timer to allow the GC to know these are unimportant
if (entry.getValue().get() == null) {
toClearCache.add(entry.getKey());
}
}
for (UUID id : toClearCache) {
secondaryPlayerFlagTrackerCache.remove(id);
}
}

private static int secondaryCleanTicker = 0;

public static void cleanCache() {
if (cacheTimeoutSeconds == -1) {
return;
}
if (secondaryCleanTicker++ > 10) {
cleanSecondaryCache();
}
long timeNow = System.currentTimeMillis();
for (Map.Entry<UUID, CachedPlayerFlag> entry : playerFlagTrackerCache.entrySet()) {
if (cacheTimeoutSeconds > 0 && entry.getValue().lastAccessed + (cacheTimeoutSeconds * 1000) < timeNow) {
Expand Down

0 comments on commit cdf9c9d

Please sign in to comment.