Unified cache implementation. Using references in events.

This commit is contained in:
2025-05-26 18:58:20 +01:00
parent dbe3f19790
commit eba233a983
4 changed files with 26 additions and 28 deletions

View File

@ -132,8 +132,8 @@ public final class Config {
private static void onConfigReload() {
// Clear cache
OperationCache.clearBlockBreakCache();
OperationCache.clearEntityKillCache();
OperationCache.blockBreakCache.clear();
OperationCache.entityKillCache.clear();
// Parse all block break global operations
blockBreakGlobalOperationItems = new ArrayList<>();

View File

@ -15,6 +15,8 @@ import java.time.Instant;
import java.util.List;
public class OnBlockBreakEventHandler {
private final OperationCache cache = OperationCache.blockBreakCache;
@SubscribeEvent
public void OnBlockBreakEvent(BlockEvent.BreakEvent event) {
Instant start = Instant.now();
@ -31,7 +33,7 @@ public class OnBlockBreakEventHandler {
// Collect operations
List<OperationItem> operations = null;
if (Config.Server.optimizationUseCache.get()) {
operations = OperationCache.getBlockBreakCacheEntry(block_id);
operations = cache.getEntry(block_id);
}
if (operations == null) {
@ -46,7 +48,7 @@ public class OnBlockBreakEventHandler {
// Save operations to cache
if (Config.Server.optimizationUseCache.get()) {
OperationCache.addBlockBreakCacheEntry(block_id, operations);
cache.addEntry(block_id, operations);
}
}
@ -61,6 +63,7 @@ public class OnBlockBreakEventHandler {
XpTools.LOGGER.debug("Completed block break event:");
XpTools.LOGGER.debug("\tOperations: {}", operations);
XpTools.LOGGER.debug("\tUsed cache: {}", usedCache);
XpTools.LOGGER.debug("\tCache entries: {}", cache.size());
XpTools.LOGGER.debug("\tTime taken (nano seconds): {}", Duration.between(start, Instant.now()).toNanos());
XpTools.LOGGER.debug("\tXP: {} -> {}", event.getExpToDrop(), xpToDrop);
}

View File

@ -15,6 +15,8 @@ import java.time.Instant;
import java.util.List;
public class OnLivingExperienceDropEventHandler {
private final OperationCache cache = OperationCache.entityKillCache;
@SubscribeEvent
public void onLivingExperienceDropEvent(LivingExperienceDropEvent event) {
Instant start = Instant.now();
@ -31,7 +33,7 @@ public class OnLivingExperienceDropEventHandler {
// Collect operations
List<OperationItem> operations = null;
if (Config.Server.optimizationUseCache.get()) {
operations = OperationCache.getEntityKillCacheEntry(entity_id);
operations = cache.getEntry(entity_id);
}
if (operations == null) {
@ -46,7 +48,7 @@ public class OnLivingExperienceDropEventHandler {
// Save operations to cache
if (Config.Server.optimizationUseCache.get()) {
OperationCache.addEntityKillCacheEntry(entity_id, operations);
cache.addEntry(entity_id, operations);
}
}
@ -61,6 +63,7 @@ public class OnLivingExperienceDropEventHandler {
XpTools.LOGGER.debug("Completed entity kill event:");
XpTools.LOGGER.debug("\tOperations: {}", operations);
XpTools.LOGGER.debug("\tUsed cache: {}", usedCache);
XpTools.LOGGER.debug("\tCache entries: {}", cache.size());
XpTools.LOGGER.debug("\tTime taken (nano seconds): {}", Duration.between(start, Instant.now()).toNanos());
XpTools.LOGGER.debug("\tXP: {} -> {}", event.getDroppedExperience(), xpToDrop);
}

View File

@ -6,36 +6,28 @@ import java.util.HashMap;
import java.util.List;
public class OperationCache {
private static HashMap<String, List<OperationItem>> blockBreakCache;
private static HashMap<String, List<OperationItem>> entityKillCache;
public static OperationCache blockBreakCache = new OperationCache();
public static OperationCache entityKillCache = new OperationCache();
private HashMap<String, List<OperationItem>> cache = new HashMap<>();
public static void clearBlockBreakCache() {
blockBreakCache = new HashMap<>();
private OperationCache() {}
public void clear() {
cache = new HashMap<>();
}
public static void clearEntityKillCache() {
entityKillCache = new HashMap<>();
}
public static @Nullable List<OperationItem> getBlockBreakCacheEntry(String key) {
if (blockBreakCache.containsKey(key)) {
return new ArrayList<>(blockBreakCache.get(key));
public @Nullable List<OperationItem> getEntry(String key) {
if (cache.containsKey(key)) {
return new ArrayList<>(cache.get(key));
}
return null;
}
public static @Nullable List<OperationItem> getEntityKillCacheEntry(String key) {
if (entityKillCache.containsKey(key)) {
return new ArrayList<>(entityKillCache.get(key));
}
return null;
public void addEntry(String key, List<OperationItem> value) {
cache.putIfAbsent(key, new ArrayList<>(value));
}
public static void addBlockBreakCacheEntry(String key, List<OperationItem> value) {
blockBreakCache.putIfAbsent(key, new ArrayList<>(value));
}
public static void addEntityKillCacheEntry(String key, List<OperationItem> value) {
entityKillCache.putIfAbsent(key, new ArrayList<>(value));
public int size() {
return cache.size();
}
}