mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-28 05:35:26 +01:00
Remove unnecessary extra cache in ContextManager
This commit is contained in:
parent
8618a67e2e
commit
3d2115ff3c
@ -29,7 +29,6 @@ import lombok.NonNull;
|
|||||||
|
|
||||||
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.github.benmanes.caffeine.cache.RemovalListener;
|
|
||||||
|
|
||||||
import me.lucko.luckperms.api.Contexts;
|
import me.lucko.luckperms.api.Contexts;
|
||||||
import me.lucko.luckperms.api.context.ContextCalculator;
|
import me.lucko.luckperms.api.context.ContextCalculator;
|
||||||
@ -40,56 +39,75 @@ import java.util.List;
|
|||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manages {@link ContextCalculator}s, and calculates applicable contexts for a
|
||||||
|
* given type.
|
||||||
|
*
|
||||||
|
* @param <T> the calculator type
|
||||||
|
*/
|
||||||
public abstract class ContextManager<T> {
|
public abstract class ContextManager<T> {
|
||||||
|
|
||||||
private final List<ContextCalculator<T>> calculators = new CopyOnWriteArrayList<>();
|
private final List<ContextCalculator<T>> calculators = new CopyOnWriteArrayList<>();
|
||||||
private final List<ContextCalculator<?>> staticCalculators = new CopyOnWriteArrayList<>();
|
private final List<ContextCalculator<?>> staticCalculators = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
private final LoadingCache<T, ImmutableContextSet> activeContextCache = Caffeine.newBuilder()
|
// caches context lookups
|
||||||
|
private final LoadingCache<T, Contexts> lookupCache = Caffeine.newBuilder()
|
||||||
.weakKeys()
|
.weakKeys()
|
||||||
.expireAfterWrite(50L, TimeUnit.MILLISECONDS)
|
.expireAfterWrite(50L, TimeUnit.MILLISECONDS) // expire roughly every tick
|
||||||
.removalListener((RemovalListener<T, ImmutableContextSet>) (t, contextSet, removalCause) -> {
|
.build(subject -> {
|
||||||
if (t != null) {
|
MutableContextSet accumulator = MutableContextSet.create();
|
||||||
invalidateContextsCache(t);
|
calculateApplicableContext(subject, accumulator);
|
||||||
}
|
|
||||||
})
|
|
||||||
.build(t -> calculateApplicableContext(t, MutableContextSet.create()).makeImmutable());
|
|
||||||
|
|
||||||
private final LoadingCache<T, Contexts> contextsCache = Caffeine.newBuilder()
|
ImmutableContextSet ret = accumulator.makeImmutable();
|
||||||
.weakKeys()
|
return formContexts(subject, ret);
|
||||||
.expireAfterWrite(100L, TimeUnit.MILLISECONDS)
|
});
|
||||||
.build(t -> formContexts(t, getApplicableContext(t)));
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Queries the ContextManager for current context values for the subject.
|
||||||
|
*
|
||||||
|
* @param subject the subject
|
||||||
|
* @return the applicable context for the subject
|
||||||
|
*/
|
||||||
public ImmutableContextSet getApplicableContext(@NonNull T subject) {
|
public ImmutableContextSet getApplicableContext(@NonNull T subject) {
|
||||||
return activeContextCache.get(subject);
|
// this is actually already immutable, but the Contexts method signature returns the interface.
|
||||||
|
return getApplicableContexts(subject).getContexts().makeImmutable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Queries the ContextManager for current context values for the subject.
|
||||||
|
*
|
||||||
|
* @param subject the subject
|
||||||
|
* @return the applicable context for the subject
|
||||||
|
*/
|
||||||
public Contexts getApplicableContexts(@NonNull T subject) {
|
public Contexts getApplicableContexts(@NonNull T subject) {
|
||||||
return contextsCache.get(subject);
|
return lookupCache.get(subject);
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract Contexts formContexts(T t, ImmutableContextSet contextSet);
|
/**
|
||||||
|
* Forms a {@link Contexts} instance from an {@link ImmutableContextSet}.
|
||||||
private MutableContextSet calculateApplicableContext(T subject, MutableContextSet accumulator) {
|
*
|
||||||
for (ContextCalculator<T> calculator : calculators) {
|
* @param subject the subject
|
||||||
try {
|
* @param contextSet the context set
|
||||||
calculator.giveApplicableContext(subject, accumulator);
|
* @return a contexts instance
|
||||||
} catch (Exception e) {
|
*/
|
||||||
new RuntimeException("Exception thrown by ContextCalculator: " + calculator.getClass().getName(), e).printStackTrace();
|
public abstract Contexts formContexts(T subject, ImmutableContextSet contextSet);
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return accumulator;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void invalidateContextsCache(T t) {
|
|
||||||
contextsCache.invalidate(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a context calculator with the manager.
|
||||||
|
*
|
||||||
|
* @param calculator the calculator
|
||||||
|
*/
|
||||||
public void registerCalculator(ContextCalculator<T> calculator) {
|
public void registerCalculator(ContextCalculator<T> calculator) {
|
||||||
registerCalculator(calculator, false);
|
registerCalculator(calculator, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a context calculator with the manager.
|
||||||
|
*
|
||||||
|
* @param calculator the calculator
|
||||||
|
* @param isStatic if the calculator is static. (if it allows a null subject parameter)
|
||||||
|
*/
|
||||||
public void registerCalculator(ContextCalculator<T> calculator, boolean isStatic) {
|
public void registerCalculator(ContextCalculator<T> calculator, boolean isStatic) {
|
||||||
// calculators registered first should have priority (and be checked last.)
|
// calculators registered first should have priority (and be checked last.)
|
||||||
calculators.add(0, calculator);
|
calculators.add(0, calculator);
|
||||||
@ -99,6 +117,11 @@ public abstract class ContextManager<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the contexts from the static calculators in this manager.
|
||||||
|
*
|
||||||
|
* @return the current active static contexts
|
||||||
|
*/
|
||||||
public ImmutableContextSet getStaticContexts() {
|
public ImmutableContextSet getStaticContexts() {
|
||||||
MutableContextSet accumulator = MutableContextSet.create();
|
MutableContextSet accumulator = MutableContextSet.create();
|
||||||
for (ContextCalculator<?> calculator : staticCalculators) {
|
for (ContextCalculator<?> calculator : staticCalculators) {
|
||||||
@ -107,11 +130,34 @@ public abstract class ContextManager<T> {
|
|||||||
return accumulator.makeImmutable();
|
return accumulator.makeImmutable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invalidates the lookup cache for a given subject
|
||||||
|
*
|
||||||
|
* @param subject the subject
|
||||||
|
*/
|
||||||
public void invalidateCache(@NonNull T subject){
|
public void invalidateCache(@NonNull T subject){
|
||||||
activeContextCache.invalidate(subject);
|
lookupCache.invalidate(subject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the number of calculators registered with the manager.
|
||||||
|
*
|
||||||
|
* @return the number of calculators registered
|
||||||
|
*/
|
||||||
public int getCalculatorsSize() {
|
public int getCalculatorsSize() {
|
||||||
return calculators.size();
|
return calculators.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// iterates the calculators in this manager and accumulates contexts from them all.
|
||||||
|
private void calculateApplicableContext(T subject, MutableContextSet accumulator) {
|
||||||
|
for (ContextCalculator<T> calculator : calculators) {
|
||||||
|
try {
|
||||||
|
calculator.giveApplicableContext(subject, accumulator);
|
||||||
|
} catch (Exception e) {
|
||||||
|
new RuntimeException("Exception thrown by ContextCalculator: " + calculator.getClass().getName(), e).printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user