From e349f47e664cc82449d18163bc0f3507844c248c Mon Sep 17 00:00:00 2001 From: Auxilor Date: Tue, 1 Feb 2022 14:04:00 +0000 Subject: [PATCH] Swapped out guava cache for caffeine --- build.gradle.kts | 1 + .../placeholder/PlaceholderManager.java | 23 +++----- .../java/com/willfp/eco/core/items/Items.java | 29 ++++----- .../java/com/willfp/eco/util/StringUtils.java | 59 +++++++------------ .../core-plugin/src/main/resources/plugin.yml | 3 +- 5 files changed, 42 insertions(+), 73 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index fd89d7be..00612c98 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 { diff --git a/eco-api/src/main/java/com/willfp/eco/core/integrations/placeholder/PlaceholderManager.java b/eco-api/src/main/java/com/willfp/eco/core/integrations/placeholder/PlaceholderManager.java index c702fe15..a590a7b5 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/integrations/placeholder/PlaceholderManager.java +++ b/eco-api/src/main/java/com/willfp/eco/core/integrations/placeholder/PlaceholderManager.java @@ -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 PLACEHOLDER_CACHE = CacheBuilder.newBuilder() + private static final LoadingCache 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)); } /** diff --git a/eco-api/src/main/java/com/willfp/eco/core/items/Items.java b/eco-api/src/main/java/com/willfp/eco/core/items/Items.java index cfaa293b..7e9d5c5e 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/items/Items.java +++ b/eco-api/src/main/java/com/willfp/eco/core/items/Items.java @@ -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> CACHE = CacheBuilder.newBuilder() + private static final LoadingCache> CACHE = Caffeine.newBuilder() .expireAfterAccess(5, TimeUnit.MINUTES) .build( - new CacheLoader<>() { - @Override - @NotNull - public Optional 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); } /** diff --git a/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java b/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java index 9edaa4e5..378067d1 100644 --- a/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java +++ b/eco-api/src/main/java/com/willfp/eco/util/StringUtils.java @@ -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 STRING_FORMAT_CACHE = CacheBuilder.newBuilder() + private static final LoadingCache 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 JSON_TO_LEGACY = CacheBuilder.newBuilder() + private static final LoadingCache 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 LEGACY_TO_JSON = CacheBuilder.newBuilder() + private static final LoadingCache 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); } /** diff --git a/eco-core/core-plugin/src/main/resources/plugin.yml b/eco-core/core-plugin/src/main/resources/plugin.yml index 076772c1..444d86c0 100644 --- a/eco-core/core-plugin/src/main/resources/plugin.yml +++ b/eco-core/core-plugin/src/main/resources/plugin.yml @@ -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' \ No newline at end of file + - 'org.objenesis:objenesis:3.2' + - 'com.github.ben-manes.caffeine:caffeine:3.0.5' \ No newline at end of file