Added guava caches to performance-sensitive components
This commit is contained in:
@@ -6,6 +6,7 @@ import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
@@ -141,4 +142,21 @@ public class PlaceholderEntry {
|
||||
public void register() {
|
||||
PlaceholderManager.registerPlaceholder(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(@Nullable final Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof PlaceholderEntry entry)) {
|
||||
return false;
|
||||
}
|
||||
return Objects.equals(this.getIdentifier(), entry.getIdentifier())
|
||||
&& Objects.equals(this.getPlugin(), entry.getPlugin());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(this.getIdentifier(), this.getPlugin());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.willfp.eco.core.integrations.placeholder;
|
||||
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.willfp.eco.core.Eco;
|
||||
import com.willfp.eco.core.EcoPlugin;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -12,6 +15,7 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Class to handle placeholder integrations.
|
||||
@@ -27,6 +31,21 @@ public final class PlaceholderManager {
|
||||
*/
|
||||
private static final Set<PlaceholderIntegration> REGISTERED_INTEGRATIONS = new HashSet<>();
|
||||
|
||||
/**
|
||||
* Placeholder Cache.
|
||||
*/
|
||||
private static final LoadingCache<EntryWithPlayer, String> PLACEHOLDER_CACHE = CacheBuilder.newBuilder()
|
||||
.expireAfterWrite(50, TimeUnit.MILLISECONDS)
|
||||
.build(
|
||||
new CacheLoader<>() {
|
||||
@Override
|
||||
@NotNull
|
||||
public String load(@NotNull final EntryWithPlayer key) {
|
||||
return key.entry.getResult(key.player);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Register a new placeholder integration.
|
||||
*
|
||||
@@ -94,7 +113,7 @@ public final class PlaceholderManager {
|
||||
return "";
|
||||
}
|
||||
|
||||
return entry.getResult(player);
|
||||
return PLACEHOLDER_CACHE.getUnchecked(new EntryWithPlayer(entry, player));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,6 +147,11 @@ public final class PlaceholderManager {
|
||||
return found;
|
||||
}
|
||||
|
||||
private static record EntryWithPlayer(@NotNull PlaceholderEntry entry,
|
||||
@Nullable Player player) {
|
||||
|
||||
}
|
||||
|
||||
private PlaceholderManager() {
|
||||
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.willfp.eco.util;
|
||||
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
@@ -20,6 +23,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -66,6 +70,45 @@ public final class StringUtils {
|
||||
.emitLegacyHoverEvent()
|
||||
.build();
|
||||
|
||||
/**
|
||||
* Json -> Legacy Cache.
|
||||
*/
|
||||
private static final LoadingCache<String, String> JSON_TO_LEGACY = CacheBuilder.newBuilder()
|
||||
.expireAfterAccess(1, TimeUnit.SECONDS)
|
||||
.build(
|
||||
new CacheLoader<>() {
|
||||
@Override
|
||||
@NotNull
|
||||
public String load(@NotNull final String json) {
|
||||
try {
|
||||
Component component = GSON_COMPONENT_SERIALIZER.deserialize(json);
|
||||
return LEGACY_COMPONENT_SERIALIZER.serialize(component);
|
||||
} catch (JsonSyntaxException e) {
|
||||
return json;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Legacy -> Json Cache.
|
||||
*/
|
||||
private static final LoadingCache<String, String> LEGACY_TO_JSON = CacheBuilder.newBuilder()
|
||||
.expireAfterAccess(1, TimeUnit.SECONDS)
|
||||
.build(
|
||||
new CacheLoader<>() {
|
||||
@Override
|
||||
@NotNull
|
||||
public String load(@NotNull final String legacy) {
|
||||
return GSON_COMPONENT_SERIALIZER.serialize(
|
||||
Component.empty().decoration(TextDecoration.ITALIC, false).append(
|
||||
LEGACY_COMPONENT_SERIALIZER.deserialize(legacy)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Color map.
|
||||
*/
|
||||
@@ -423,11 +466,8 @@ public final class StringUtils {
|
||||
if (legacy == null) {
|
||||
processed = "";
|
||||
}
|
||||
return GSON_COMPONENT_SERIALIZER.serialize(
|
||||
Component.empty().decoration(TextDecoration.ITALIC, false).append(
|
||||
LEGACY_COMPONENT_SERIALIZER.deserialize(processed)
|
||||
)
|
||||
);
|
||||
|
||||
return LEGACY_TO_JSON.getUnchecked(processed);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -442,12 +482,7 @@ public final class StringUtils {
|
||||
return "";
|
||||
}
|
||||
|
||||
try {
|
||||
Component component = GSON_COMPONENT_SERIALIZER.deserialize(json);
|
||||
return LEGACY_COMPONENT_SERIALIZER.serialize(component);
|
||||
} catch (JsonSyntaxException e) {
|
||||
return json;
|
||||
}
|
||||
return JSON_TO_LEGACY.getUnchecked(json);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user