From 118d79f3ade5004e634e3b14682133746f3328bb Mon Sep 17 00:00:00 2001 From: "Luis C.M" <42545778+c0508383@users.noreply.github.com> Date: Wed, 31 May 2023 00:35:57 -0500 Subject: [PATCH] Added synchronization blocks to functions retrieving objects from CacheHashMaps and made the API use one for caching item stacks as well. --- .../java/noppes/npcs/client/ClientCache.java | 11 ++-- .../java/noppes/npcs/scripted/NpcAPI.java | 50 +++++++------------ .../npcs/scripted/item/ScriptItemStack.java | 1 - .../java/noppes/npcs/util/CacheHashMap.java | 25 ++++------ 4 files changed, 34 insertions(+), 53 deletions(-) diff --git a/src/main/java/noppes/npcs/client/ClientCache.java b/src/main/java/noppes/npcs/client/ClientCache.java index 43fb33bd7..85b82e844 100644 --- a/src/main/java/noppes/npcs/client/ClientCache.java +++ b/src/main/java/noppes/npcs/client/ClientCache.java @@ -4,13 +4,14 @@ import noppes.npcs.client.renderer.customitem.ImageData; import noppes.npcs.util.CacheHashMap; public class ClientCache { - private static final CacheHashMap> imageDataCache = new CacheHashMap<>(10*1000); + private static final CacheHashMap> imageDataCache = new CacheHashMap<>(10*60*1000); public static ImageData getImageData(String directory) { - if (!imageDataCache.containsKey(directory)) { - ImageData imageData = new ImageData(directory); - imageDataCache.put(directory, new CacheHashMap.CachedObject<>(imageData)); + synchronized (imageDataCache) { + if (!imageDataCache.containsKey(directory)) { + imageDataCache.put(directory, new CacheHashMap.CachedObject<>(new ImageData(directory))); + } + return imageDataCache.get(directory).getObject(); } - return imageDataCache.get(directory).getObject(); } } diff --git a/src/main/java/noppes/npcs/scripted/NpcAPI.java b/src/main/java/noppes/npcs/scripted/NpcAPI.java index 96492e409..bec836f63 100644 --- a/src/main/java/noppes/npcs/scripted/NpcAPI.java +++ b/src/main/java/noppes/npcs/scripted/NpcAPI.java @@ -61,6 +61,7 @@ import noppes.npcs.scripted.entity.*; import noppes.npcs.scripted.gui.ScriptGui; import noppes.npcs.scripted.item.*; import noppes.npcs.scripted.overlay.ScriptOverlay; +import noppes.npcs.util.CacheHashMap; import noppes.npcs.util.JsonException; import noppes.npcs.util.LRUHashMap; import noppes.npcs.util.NBTJsonUtil; @@ -70,7 +71,7 @@ import java.util.*; public class NpcAPI extends AbstractNpcAPI { private static final Map worldCache = new LRUHashMap<>(10); - private static final HashMap scriptItemCache = new HashMap<>(); + private static final CacheHashMap> scriptItemCache = new CacheHashMap<>(1000); public static final HashMap engineObjects = new HashMap<>(); public static final EventBus EVENT_BUS = new EventBus(); private static AbstractNpcAPI instance = null; @@ -353,40 +354,27 @@ public class NpcAPI extends AbstractNpcAPI { if(itemstack == null) return null; - ScriptItemStack scriptStack; - if (scriptItemCache.containsKey(itemstack)) { - scriptStack = scriptItemCache.get(itemstack); - } else { - if (itemstack.getItem() instanceof ItemScripted) { - scriptStack = new ScriptCustomItem(itemstack); - } else if (itemstack.getItem() instanceof ItemArmor) { - scriptStack = new ScriptItemArmor(itemstack); - } else if (itemstack.getItem() instanceof ItemBook) { - scriptStack = new ScriptItemBook(itemstack); - } else if (itemstack.getItem() instanceof ItemBlock) { - scriptStack = new ScriptItemBlock(itemstack); - } else { - scriptStack = new ScriptItemStack(itemstack); - } - } - - long time = System.currentTimeMillis(); - scriptStack.lastAccessed = time; - synchronized (scriptItemCache) { - List keysToRemove = new ArrayList<>(); - for (Map.Entry entry : scriptItemCache.entrySet()) { - if (time - entry.getValue().lastAccessed > 20000) { - keysToRemove.add(entry.getKey()); + ScriptItemStack scriptStack; + if (scriptItemCache.containsKey(itemstack)) { + scriptStack = scriptItemCache.get(itemstack).getObject(); + } else { + if (itemstack.getItem() instanceof ItemScripted) { + scriptStack = new ScriptCustomItem(itemstack); + } else if (itemstack.getItem() instanceof ItemArmor) { + scriptStack = new ScriptItemArmor(itemstack); + } else if (itemstack.getItem() instanceof ItemBook) { + scriptStack = new ScriptItemBook(itemstack); + } else if (itemstack.getItem() instanceof ItemBlock) { + scriptStack = new ScriptItemBlock(itemstack); + } else { + scriptStack = new ScriptItemStack(itemstack); } + scriptItemCache.put(itemstack, new CacheHashMap.CachedObject<>(scriptStack)); } - for (ItemStack key : keysToRemove) { - scriptItemCache.remove(key); - } - scriptItemCache.put(itemstack, scriptStack); - } - return scriptStack; + return scriptStack; + } } public IItemStack createItemFromNBT(INbt nbt) { diff --git a/src/main/java/noppes/npcs/scripted/item/ScriptItemStack.java b/src/main/java/noppes/npcs/scripted/item/ScriptItemStack.java index 271d54814..d0a43e920 100644 --- a/src/main/java/noppes/npcs/scripted/item/ScriptItemStack.java +++ b/src/main/java/noppes/npcs/scripted/item/ScriptItemStack.java @@ -28,7 +28,6 @@ import java.util.List; import java.util.Map; public class ScriptItemStack implements IItemStack { - public long lastAccessed; public ItemStack item; public ScriptItemStack(ItemStack item){ diff --git a/src/main/java/noppes/npcs/util/CacheHashMap.java b/src/main/java/noppes/npcs/util/CacheHashMap.java index 2686b6d0b..44ef76a3a 100644 --- a/src/main/java/noppes/npcs/util/CacheHashMap.java +++ b/src/main/java/noppes/npcs/util/CacheHashMap.java @@ -11,25 +11,18 @@ public class CacheHashMap> extends Has @Override public V get(Object key) { - synchronized (this) { - List keysToRemove = new ArrayList<>(); - Set> entrySet = this.entrySet(); - for (Map.Entry entry : entrySet) { - if (!Objects.equals(key,entry.getKey())) { - long timeDiff = entry.getValue().timeSinceAccessed(); - if (timeDiff > this.maxCacheTime) { - keysToRemove.add(entry.getKey()); - } - } - } - for (K k : keysToRemove) { - this.remove(k); - } - } - V object = super.get(key); object.updateTimeAccessed(); + Iterator> iterator = this.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + long timeDiff = entry.getValue().timeSinceAccessed(); + if (timeDiff > this.maxCacheTime) { + iterator.remove(); + } + } + return object; }