Swapped out guava cache for caffeine

This commit is contained in:
Auxilor
2022-02-01 14:04:00 +00:00
parent f710a69455
commit e349f47e66
5 changed files with 42 additions and 73 deletions

View File

@@ -86,6 +86,7 @@ allprojects {
// Other
compileOnly("com.google.guava:guava:31.0.1-jre")
compileOnly("com.github.ben-manes.caffeine:caffeine:3.0.5")
}
tasks.withType<JavaCompile> {

View File

@@ -1,8 +1,7 @@
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.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.willfp.eco.core.Eco;
import com.willfp.eco.core.EcoPlugin;
import org.bukkit.entity.Player;
@@ -34,22 +33,14 @@ public final class PlaceholderManager {
/**
* Placeholder Cache.
*/
private static final LoadingCache<EntryWithPlayer, String> PLACEHOLDER_CACHE = CacheBuilder.newBuilder()
private static final LoadingCache<EntryWithPlayer, String> PLACEHOLDER_CACHE = Caffeine.newBuilder()
.expireAfterWrite(50, TimeUnit.MILLISECONDS)
.build(
new CacheLoader<>() {
@Override
@NotNull
public String load(@NotNull final EntryWithPlayer key) {
return key.entry.getResult(key.player);
}
}
);
.build(key -> key.entry.getResult(key.player));
/**
* Register a new placeholder integration.
*
* @param integration The {@link PlaceholderIntegration} to register.
* @param integration The {@link com.willfp.eco.core.integrations.placeholder.PlaceholderIntegration} to register.
*/
public static void addIntegration(@NotNull final PlaceholderIntegration integration) {
integration.registerIntegration();
@@ -59,7 +50,7 @@ public final class PlaceholderManager {
/**
* Register a placeholder.
*
* @param expansion The {@link PlaceholderEntry} to register.
* @param expansion The {@link com.willfp.eco.core.integrations.placeholder.PlaceholderEntry} to register.
*/
public static void registerPlaceholder(@NotNull final PlaceholderEntry expansion) {
EcoPlugin plugin = expansion.getPlugin() == null ? Eco.getHandler().getEcoPlugin() : expansion.getPlugin();
@@ -113,7 +104,7 @@ public final class PlaceholderManager {
return "";
}
return PLACEHOLDER_CACHE.getUnchecked(new EntryWithPlayer(entry, player));
return PLACEHOLDER_CACHE.get(new EntryWithPlayer(entry, player));
}
/**

View File

@@ -1,8 +1,7 @@
package com.willfp.eco.core.items;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.willfp.eco.core.items.args.LookupArgParser;
import com.willfp.eco.core.items.provider.ItemProvider;
import com.willfp.eco.core.recipe.parts.EmptyTestableItem;
@@ -42,23 +41,19 @@ public final class Items {
/**
* Cached custom item lookups, using {@link HashedItem}.
*/
private static final LoadingCache<HashedItem, Optional<TestableItem>> CACHE = CacheBuilder.newBuilder()
private static final LoadingCache<HashedItem, Optional<TestableItem>> CACHE = Caffeine.newBuilder()
.expireAfterAccess(5, TimeUnit.MINUTES)
.build(
new CacheLoader<>() {
@Override
@NotNull
public Optional<TestableItem> load(@NotNull final HashedItem key) {
TestableItem match = null;
for (TestableItem item : REGISTRY.values()) {
if (item.matches(key.getItem())) {
match = item;
break;
}
key -> {
TestableItem match = null;
for (TestableItem item : REGISTRY.values()) {
if (item.matches(key.getItem())) {
match = item;
break;
}
return Optional.ofNullable(match);
}
return Optional.ofNullable(match);
}
);
@@ -315,7 +310,7 @@ public final class Items {
*/
@Nullable
public static CustomItem getCustomItem(@NotNull final ItemStack itemStack) {
return CACHE.getUnchecked(HashedItem.of(itemStack)).map(Items::getOrWrap).orElse(null);
return CACHE.get(HashedItem.of(itemStack)).map(Items::getOrWrap).orElse(null);
}
/**

View File

@@ -1,8 +1,7 @@
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.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonSyntaxException;
@@ -73,34 +72,22 @@ public final class StringUtils {
/**
* String format cache.
*/
private static final LoadingCache<FormatLookup, String> STRING_FORMAT_CACHE = CacheBuilder.newBuilder()
private static final LoadingCache<FormatLookup, String> STRING_FORMAT_CACHE = Caffeine.newBuilder()
.expireAfterAccess(10, TimeUnit.SECONDS)
.build(
new CacheLoader<>() {
@Override
@NotNull
public String load(@NotNull final FormatLookup key) {
return format(key);
}
}
);
.build(StringUtils::format);
/**
* Json -> Legacy Cache.
*/
private static final LoadingCache<String, String> JSON_TO_LEGACY = CacheBuilder.newBuilder()
private static final LoadingCache<String, String> JSON_TO_LEGACY = Caffeine.newBuilder()
.expireAfterAccess(10, 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;
}
json -> {
try {
Component component = GSON_COMPONENT_SERIALIZER.deserialize(json);
return LEGACY_COMPONENT_SERIALIZER.serialize(component);
} catch (JsonSyntaxException e) {
return json;
}
}
);
@@ -108,20 +95,14 @@ public final class StringUtils {
/**
* Legacy -> Json Cache.
*/
private static final LoadingCache<String, String> LEGACY_TO_JSON = CacheBuilder.newBuilder()
private static final LoadingCache<String, String> LEGACY_TO_JSON = Caffeine.newBuilder()
.expireAfterAccess(10, 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)
)
);
}
}
legacy -> GSON_COMPONENT_SERIALIZER.serialize(
Component.empty().decoration(TextDecoration.ITALIC, false).append(
LEGACY_COMPONENT_SERIALIZER.deserialize(legacy)
)
)
);
/**
@@ -331,7 +312,7 @@ public final class StringUtils {
processedMessage = PlaceholderManager.translatePlaceholders(processedMessage, player);
}
FormatLookup lookup = new FormatLookup(processedMessage, player);
return STRING_FORMAT_CACHE.getUnchecked(lookup);
return STRING_FORMAT_CACHE.get(lookup);
}
private static String format(@NotNull final FormatLookup lookup) {
@@ -488,7 +469,7 @@ public final class StringUtils {
processed = "";
}
return LEGACY_TO_JSON.getUnchecked(processed);
return LEGACY_TO_JSON.get(processed);
}
/**
@@ -503,7 +484,7 @@ public final class StringUtils {
return "";
}
return JSON_TO_LEGACY.getUnchecked(json);
return JSON_TO_LEGACY.get(json);
}
/**

View File

@@ -57,4 +57,5 @@ libraries:
- 'mysql:mysql-connector-java:8.0.25'
- 'com.google.guava:guava:31.0.1-jre'
- 'com.zaxxer:HikariCP:5.0.0'
- 'org.objenesis:objenesis:3.2'
- 'org.objenesis:objenesis:3.2'
- 'com.github.ben-manes.caffeine:caffeine:3.0.5'