Apply improvements to the caches in AbstractContextManager (#929)
This commit is contained in:
parent
97121bc719
commit
4cdff14c7c
@ -28,6 +28,8 @@ package me.lucko.luckperms.common.contexts;
|
|||||||
import com.github.benmanes.caffeine.cache.CacheLoader;
|
import com.github.benmanes.caffeine.cache.CacheLoader;
|
||||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||||
import com.github.benmanes.caffeine.cache.LoadingCache;
|
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 com.google.common.collect.ImmutableList;
|
||||||
|
|
||||||
import me.lucko.luckperms.api.Contexts;
|
import me.lucko.luckperms.api.Contexts;
|
||||||
@ -64,18 +66,12 @@ public abstract class AbstractContextManager<T> implements ContextManager<T> {
|
|||||||
|
|
||||||
// caches context lookups
|
// caches context lookups
|
||||||
private final LoadingCache<T, Contexts> lookupCache = Caffeine.newBuilder()
|
private final LoadingCache<T, Contexts> lookupCache = Caffeine.newBuilder()
|
||||||
.weakKeys()
|
|
||||||
.expireAfterWrite(50L, TimeUnit.MILLISECONDS) // expire roughly every tick
|
.expireAfterWrite(50L, TimeUnit.MILLISECONDS) // expire roughly every tick
|
||||||
.build(new Loader());
|
.build(new Loader());
|
||||||
|
|
||||||
// caches static context lookups
|
// caches static context lookups
|
||||||
private final LoadingCache<Object, Contexts> staticLookupCache = Caffeine.newBuilder()
|
@SuppressWarnings("Guava")
|
||||||
.initialCapacity(1)
|
private final Supplier<Contexts> staticLookupCache = Suppliers.memoizeWithExpiration(new StaticLoader(), 50L, TimeUnit.MILLISECONDS);
|
||||||
.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();
|
|
||||||
|
|
||||||
protected AbstractContextManager(LuckPermsPlugin plugin, Class<T> subjectClass) {
|
protected AbstractContextManager(LuckPermsPlugin plugin, Class<T> subjectClass) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
@ -125,7 +121,7 @@ public abstract class AbstractContextManager<T> implements ContextManager<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Contexts getStaticContexts() {
|
public Contexts getStaticContexts() {
|
||||||
return this.staticLookupCache.get(this.staticCacheKey);
|
return this.staticLookupCache.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -211,9 +207,9 @@ public abstract class AbstractContextManager<T> implements ContextManager<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private final class StaticLoader implements CacheLoader<Object, Contexts> {
|
private final class StaticLoader implements Supplier<Contexts> {
|
||||||
@Override
|
@Override
|
||||||
public Contexts load(@Nonnull Object o) {
|
public Contexts get() {
|
||||||
MutableContextSet accumulator = MutableContextSet.create();
|
MutableContextSet accumulator = MutableContextSet.create();
|
||||||
|
|
||||||
for (StaticContextCalculator calculator : AbstractContextManager.this.staticCalculators) {
|
for (StaticContextCalculator calculator : AbstractContextManager.this.staticCalculators) {
|
||||||
|
Loading…
Reference in New Issue
Block a user