Added synchronization blocks to functions retrieving objects from CacheHashMaps and made the API use one for caching item stacks as well.

This commit is contained in:
Luis C.M committed 2023-05-31 00:35:57 -05:00
1 parent 3bc02a708e
commit 118d79f3ad
4 files changed
+34 -53

No files matched your search

@@ -4,13 +4,14 @@ import noppes.npcs.client.renderer.customitem.ImageData;
import noppes.npcs.util.CacheHashMap;
public class ClientCache {
private static final CacheHashMap<String, CacheHashMap.CachedObject<ImageData>> imageDataCache = new CacheHashMap<>(10*1000);
private static final CacheHashMap<String, CacheHashMap.CachedObject<ImageData>> 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();
}
}
+19 -31
View File
@@ -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<Integer, ScriptWorld> worldCache = new LRUHashMap<>(10);
private static final HashMap<ItemStack,ScriptItemStack> scriptItemCache = new HashMap<>();
private static final CacheHashMap<ItemStack, CacheHashMap.CachedObject<ScriptItemStack>> scriptItemCache = new CacheHashMap<>(1000);
public static final HashMap<String,Object> 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<ItemStack> keysToRemove = new ArrayList<>();
for (Map.Entry<ItemStack, ScriptItemStack> 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) {
@@ -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){
@@ -11,25 +11,18 @@ public class CacheHashMap<K, V extends CacheHashMap.CachedObject<?>> extends Has
@Override
public V get(Object key) {
synchronized (this) {
List<K> keysToRemove = new ArrayList<>();
Set<Map.Entry<K,V>> entrySet = this.entrySet();
for (Map.Entry<K,V> 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<Map.Entry<K,V>> iterator = this.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<K,V> entry = iterator.next();
long timeDiff = entry.getValue().timeSinceAccessed();
if (timeDiff > this.maxCacheTime) {
iterator.remove();
}
}
return object;
}