Unified cache implementation. Using references in events.
This commit is contained in:
@ -132,8 +132,8 @@ public final class Config {
|
|||||||
|
|
||||||
private static void onConfigReload() {
|
private static void onConfigReload() {
|
||||||
// Clear cache
|
// Clear cache
|
||||||
OperationCache.clearBlockBreakCache();
|
OperationCache.blockBreakCache.clear();
|
||||||
OperationCache.clearEntityKillCache();
|
OperationCache.entityKillCache.clear();
|
||||||
|
|
||||||
// Parse all block break global operations
|
// Parse all block break global operations
|
||||||
blockBreakGlobalOperationItems = new ArrayList<>();
|
blockBreakGlobalOperationItems = new ArrayList<>();
|
||||||
|
@ -15,6 +15,8 @@ import java.time.Instant;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class OnBlockBreakEventHandler {
|
public class OnBlockBreakEventHandler {
|
||||||
|
private final OperationCache cache = OperationCache.blockBreakCache;
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public void OnBlockBreakEvent(BlockEvent.BreakEvent event) {
|
public void OnBlockBreakEvent(BlockEvent.BreakEvent event) {
|
||||||
Instant start = Instant.now();
|
Instant start = Instant.now();
|
||||||
@ -31,7 +33,7 @@ public class OnBlockBreakEventHandler {
|
|||||||
// Collect operations
|
// Collect operations
|
||||||
List<OperationItem> operations = null;
|
List<OperationItem> operations = null;
|
||||||
if (Config.Server.optimizationUseCache.get()) {
|
if (Config.Server.optimizationUseCache.get()) {
|
||||||
operations = OperationCache.getBlockBreakCacheEntry(block_id);
|
operations = cache.getEntry(block_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (operations == null) {
|
if (operations == null) {
|
||||||
@ -46,7 +48,7 @@ public class OnBlockBreakEventHandler {
|
|||||||
|
|
||||||
// Save operations to cache
|
// Save operations to cache
|
||||||
if (Config.Server.optimizationUseCache.get()) {
|
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("Completed block break event:");
|
||||||
XpTools.LOGGER.debug("\tOperations: {}", operations);
|
XpTools.LOGGER.debug("\tOperations: {}", operations);
|
||||||
XpTools.LOGGER.debug("\tUsed cache: {}", usedCache);
|
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("\tTime taken (nano seconds): {}", Duration.between(start, Instant.now()).toNanos());
|
||||||
XpTools.LOGGER.debug("\tXP: {} -> {}", event.getExpToDrop(), xpToDrop);
|
XpTools.LOGGER.debug("\tXP: {} -> {}", event.getExpToDrop(), xpToDrop);
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,8 @@ import java.time.Instant;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class OnLivingExperienceDropEventHandler {
|
public class OnLivingExperienceDropEventHandler {
|
||||||
|
private final OperationCache cache = OperationCache.entityKillCache;
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
public void onLivingExperienceDropEvent(LivingExperienceDropEvent event) {
|
public void onLivingExperienceDropEvent(LivingExperienceDropEvent event) {
|
||||||
Instant start = Instant.now();
|
Instant start = Instant.now();
|
||||||
@ -31,7 +33,7 @@ public class OnLivingExperienceDropEventHandler {
|
|||||||
// Collect operations
|
// Collect operations
|
||||||
List<OperationItem> operations = null;
|
List<OperationItem> operations = null;
|
||||||
if (Config.Server.optimizationUseCache.get()) {
|
if (Config.Server.optimizationUseCache.get()) {
|
||||||
operations = OperationCache.getEntityKillCacheEntry(entity_id);
|
operations = cache.getEntry(entity_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (operations == null) {
|
if (operations == null) {
|
||||||
@ -46,7 +48,7 @@ public class OnLivingExperienceDropEventHandler {
|
|||||||
|
|
||||||
// Save operations to cache
|
// Save operations to cache
|
||||||
if (Config.Server.optimizationUseCache.get()) {
|
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("Completed entity kill event:");
|
||||||
XpTools.LOGGER.debug("\tOperations: {}", operations);
|
XpTools.LOGGER.debug("\tOperations: {}", operations);
|
||||||
XpTools.LOGGER.debug("\tUsed cache: {}", usedCache);
|
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("\tTime taken (nano seconds): {}", Duration.between(start, Instant.now()).toNanos());
|
||||||
XpTools.LOGGER.debug("\tXP: {} -> {}", event.getDroppedExperience(), xpToDrop);
|
XpTools.LOGGER.debug("\tXP: {} -> {}", event.getDroppedExperience(), xpToDrop);
|
||||||
}
|
}
|
||||||
|
@ -6,36 +6,28 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class OperationCache {
|
public class OperationCache {
|
||||||
private static HashMap<String, List<OperationItem>> blockBreakCache;
|
public static OperationCache blockBreakCache = new OperationCache();
|
||||||
private static HashMap<String, List<OperationItem>> entityKillCache;
|
public static OperationCache entityKillCache = new OperationCache();
|
||||||
|
private HashMap<String, List<OperationItem>> cache = new HashMap<>();
|
||||||
|
|
||||||
public static void clearBlockBreakCache() {
|
private OperationCache() {}
|
||||||
blockBreakCache = new HashMap<>();
|
|
||||||
|
public void clear() {
|
||||||
|
cache = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void clearEntityKillCache() {
|
public @Nullable List<OperationItem> getEntry(String key) {
|
||||||
entityKillCache = new HashMap<>();
|
if (cache.containsKey(key)) {
|
||||||
}
|
return new ArrayList<>(cache.get(key));
|
||||||
|
|
||||||
public static @Nullable List<OperationItem> getBlockBreakCacheEntry(String key) {
|
|
||||||
if (blockBreakCache.containsKey(key)) {
|
|
||||||
return new ArrayList<>(blockBreakCache.get(key));
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static @Nullable List<OperationItem> getEntityKillCacheEntry(String key) {
|
public void addEntry(String key, List<OperationItem> value) {
|
||||||
if (entityKillCache.containsKey(key)) {
|
cache.putIfAbsent(key, new ArrayList<>(value));
|
||||||
return new ArrayList<>(entityKillCache.get(key));
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void addBlockBreakCacheEntry(String key, List<OperationItem> value) {
|
public int size() {
|
||||||
blockBreakCache.putIfAbsent(key, new ArrayList<>(value));
|
return cache.size();
|
||||||
}
|
|
||||||
|
|
||||||
public static void addEntityKillCacheEntry(String key, List<OperationItem> value) {
|
|
||||||
entityKillCache.putIfAbsent(key, new ArrayList<>(value));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user