From 4cdff14c7c5504539f0250090fda075b388bae24 Mon Sep 17 00:00:00 2001 From: Luck Date: Wed, 25 Apr 2018 21:14:16 +0100 Subject: [PATCH] Apply improvements to the caches in AbstractContextManager (#929) --- .../contexts/AbstractContextManager.java | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/contexts/AbstractContextManager.java b/common/src/main/java/me/lucko/luckperms/common/contexts/AbstractContextManager.java index b5a1fbe9..e3847729 100644 --- a/common/src/main/java/me/lucko/luckperms/common/contexts/AbstractContextManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/contexts/AbstractContextManager.java @@ -28,6 +28,8 @@ package me.lucko.luckperms.common.contexts; import com.github.benmanes.caffeine.cache.CacheLoader; import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.LoadingCache; +import com.google.common.base.Supplier; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableList; import me.lucko.luckperms.api.Contexts; @@ -64,18 +66,12 @@ public abstract class AbstractContextManager implements ContextManager { // caches context lookups private final LoadingCache lookupCache = Caffeine.newBuilder() - .weakKeys() .expireAfterWrite(50L, TimeUnit.MILLISECONDS) // expire roughly every tick .build(new Loader()); // caches static context lookups - private final LoadingCache staticLookupCache = Caffeine.newBuilder() - .initialCapacity(1) - .expireAfterWrite(50L, TimeUnit.MILLISECONDS) // expire roughly every tick - .build(new StaticLoader()); - - // the single key used in the static lookup cache - private final Object staticCacheKey = new Object(); + @SuppressWarnings("Guava") + private final Supplier staticLookupCache = Suppliers.memoizeWithExpiration(new StaticLoader(), 50L, TimeUnit.MILLISECONDS); protected AbstractContextManager(LuckPermsPlugin plugin, Class subjectClass) { this.plugin = plugin; @@ -125,7 +121,7 @@ public abstract class AbstractContextManager implements ContextManager { @Override public Contexts getStaticContexts() { - return this.staticLookupCache.get(this.staticCacheKey); + return this.staticLookupCache.get(); } @Override @@ -211,9 +207,9 @@ public abstract class AbstractContextManager implements ContextManager { } } - private final class StaticLoader implements CacheLoader { + private final class StaticLoader implements Supplier { @Override - public Contexts load(@Nonnull Object o) { + public Contexts get() { MutableContextSet accumulator = MutableContextSet.create(); for (StaticContextCalculator calculator : AbstractContextManager.this.staticCalculators) {