mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-24 11:38:40 +01:00
Reduce the throughput on the contexts cache in ContextManager (#929)
This commit is contained in:
parent
804c884d8b
commit
a927ca659f
@ -25,10 +25,10 @@
|
|||||||
|
|
||||||
package me.lucko.luckperms.bukkit.model.permissible;
|
package me.lucko.luckperms.bukkit.model.permissible;
|
||||||
|
|
||||||
import me.lucko.luckperms.api.Contexts;
|
|
||||||
import me.lucko.luckperms.api.Tristate;
|
import me.lucko.luckperms.api.Tristate;
|
||||||
import me.lucko.luckperms.bukkit.LPBukkitPlugin;
|
import me.lucko.luckperms.bukkit.LPBukkitPlugin;
|
||||||
import me.lucko.luckperms.common.config.ConfigKeys;
|
import me.lucko.luckperms.common.config.ConfigKeys;
|
||||||
|
import me.lucko.luckperms.common.contexts.ContextsCache;
|
||||||
import me.lucko.luckperms.common.model.User;
|
import me.lucko.luckperms.common.model.User;
|
||||||
import me.lucko.luckperms.common.verbose.CheckOrigin;
|
import me.lucko.luckperms.common.verbose.CheckOrigin;
|
||||||
|
|
||||||
@ -72,6 +72,9 @@ public class LPPermissible extends PermissibleBase {
|
|||||||
// the luckperms plugin instance
|
// the luckperms plugin instance
|
||||||
private final LPBukkitPlugin plugin;
|
private final LPBukkitPlugin plugin;
|
||||||
|
|
||||||
|
// caches context lookups for the player
|
||||||
|
private final ContextsCache<Player> contextsCache;
|
||||||
|
|
||||||
// the players previous permissible. (the one they had before this one was injected)
|
// the players previous permissible. (the one they had before this one was injected)
|
||||||
private PermissibleBase oldPermissible = null;
|
private PermissibleBase oldPermissible = null;
|
||||||
|
|
||||||
@ -87,6 +90,7 @@ public class LPPermissible extends PermissibleBase {
|
|||||||
this.user = Objects.requireNonNull(user, "user");
|
this.user = Objects.requireNonNull(user, "user");
|
||||||
this.player = Objects.requireNonNull(player, "player");
|
this.player = Objects.requireNonNull(player, "player");
|
||||||
this.plugin = Objects.requireNonNull(plugin, "plugin");
|
this.plugin = Objects.requireNonNull(plugin, "plugin");
|
||||||
|
this.contextsCache = plugin.getContextManager().getCacheFor(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -95,7 +99,7 @@ public class LPPermissible extends PermissibleBase {
|
|||||||
throw new NullPointerException("permission");
|
throw new NullPointerException("permission");
|
||||||
}
|
}
|
||||||
|
|
||||||
Tristate ts = this.user.getCachedData().getPermissionData(calculateContexts()).getPermissionValue(permission, CheckOrigin.PLATFORM_LOOKUP_CHECK);
|
Tristate ts = this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getPermissionValue(permission, CheckOrigin.PLATFORM_LOOKUP_CHECK);
|
||||||
return ts != Tristate.UNDEFINED || Permission.DEFAULT_PERMISSION.getValue(isOp());
|
return ts != Tristate.UNDEFINED || Permission.DEFAULT_PERMISSION.getValue(isOp());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,7 +109,7 @@ public class LPPermissible extends PermissibleBase {
|
|||||||
throw new NullPointerException("permission");
|
throw new NullPointerException("permission");
|
||||||
}
|
}
|
||||||
|
|
||||||
Tristate ts = this.user.getCachedData().getPermissionData(calculateContexts()).getPermissionValue(permission.getName(), CheckOrigin.PLATFORM_LOOKUP_CHECK);
|
Tristate ts = this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getPermissionValue(permission.getName(), CheckOrigin.PLATFORM_LOOKUP_CHECK);
|
||||||
if (ts != Tristate.UNDEFINED) {
|
if (ts != Tristate.UNDEFINED) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -123,7 +127,7 @@ public class LPPermissible extends PermissibleBase {
|
|||||||
throw new NullPointerException("permission");
|
throw new NullPointerException("permission");
|
||||||
}
|
}
|
||||||
|
|
||||||
Tristate ts = this.user.getCachedData().getPermissionData(calculateContexts()).getPermissionValue(permission, CheckOrigin.PLATFORM_PERMISSION_CHECK);
|
Tristate ts = this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getPermissionValue(permission, CheckOrigin.PLATFORM_PERMISSION_CHECK);
|
||||||
return ts != Tristate.UNDEFINED ? ts.asBoolean() : Permission.DEFAULT_PERMISSION.getValue(isOp());
|
return ts != Tristate.UNDEFINED ? ts.asBoolean() : Permission.DEFAULT_PERMISSION.getValue(isOp());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,7 +137,7 @@ public class LPPermissible extends PermissibleBase {
|
|||||||
throw new NullPointerException("permission");
|
throw new NullPointerException("permission");
|
||||||
}
|
}
|
||||||
|
|
||||||
Tristate ts = this.user.getCachedData().getPermissionData(calculateContexts()).getPermissionValue(permission.getName(), CheckOrigin.PLATFORM_PERMISSION_CHECK);
|
Tristate ts = this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getPermissionValue(permission.getName(), CheckOrigin.PLATFORM_PERMISSION_CHECK);
|
||||||
if (ts != Tristate.UNDEFINED) {
|
if (ts != Tristate.UNDEFINED) {
|
||||||
return ts.asBoolean();
|
return ts.asBoolean();
|
||||||
}
|
}
|
||||||
@ -156,16 +160,6 @@ public class LPPermissible extends PermissibleBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Obtains a {@link Contexts} instance for the player.
|
|
||||||
* Values are determined using the plugins ContextManager.
|
|
||||||
*
|
|
||||||
* @return the calculated contexts for the player.
|
|
||||||
*/
|
|
||||||
private Contexts calculateContexts() {
|
|
||||||
return this.plugin.getContextManager().getApplicableContexts(this.player);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setOp(boolean value) {
|
public void setOp(boolean value) {
|
||||||
this.player.setOp(value);
|
this.player.setOp(value);
|
||||||
@ -173,7 +167,7 @@ public class LPPermissible extends PermissibleBase {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
|
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
|
||||||
Set<Map.Entry<String, Boolean>> permissions = this.user.getCachedData().getPermissionData(calculateContexts()).getImmutableBacking().entrySet();
|
Set<Map.Entry<String, Boolean>> permissions = this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getImmutableBacking().entrySet();
|
||||||
Set<PermissionAttachmentInfo> ret = new HashSet<>(permissions.size());
|
Set<PermissionAttachmentInfo> ret = new HashSet<>(permissions.size());
|
||||||
|
|
||||||
for (Map.Entry<String, Boolean> entry : permissions) {
|
for (Map.Entry<String, Boolean> entry : permissions) {
|
||||||
|
@ -0,0 +1,80 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of LuckPerms, licensed under the MIT License.
|
||||||
|
*
|
||||||
|
* Copyright (c) lucko (Luck) <luck@lucko.me>
|
||||||
|
* Copyright (c) contributors
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package me.lucko.luckperms.common.buffers;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An expiring supplier extension.
|
||||||
|
*
|
||||||
|
* <p>The delegate supplier is only called on executions of {@link #get()} if the
|
||||||
|
* result isn't already calculated.</p>
|
||||||
|
*
|
||||||
|
* @param <T> the supplied type
|
||||||
|
*/
|
||||||
|
public abstract class ExpiringCache<T> implements Supplier<T> {
|
||||||
|
private final long durationNanos;
|
||||||
|
|
||||||
|
private volatile T value;
|
||||||
|
|
||||||
|
// when to expire. 0 means "not yet initialized".
|
||||||
|
private volatile long expirationNanos;
|
||||||
|
|
||||||
|
protected ExpiringCache(long duration, TimeUnit unit) {
|
||||||
|
this.durationNanos = unit.toNanos(duration);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
protected abstract T supply();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T get() {
|
||||||
|
long nanos = this.expirationNanos;
|
||||||
|
long now = System.nanoTime();
|
||||||
|
|
||||||
|
if (nanos == 0 || now - nanos >= 0) {
|
||||||
|
synchronized (this) {
|
||||||
|
if (nanos == this.expirationNanos) { // recheck for lost race
|
||||||
|
// compute the value using the delegate
|
||||||
|
T t = supply();
|
||||||
|
this.value = t;
|
||||||
|
|
||||||
|
// reset expiration timer
|
||||||
|
nanos = now + this.durationNanos;
|
||||||
|
// In the very unlikely event that nanos is 0, set it to 1;
|
||||||
|
// no one will notice 1 ns of tardiness.
|
||||||
|
this.expirationNanos = (nanos == 0) ? 1 : nanos;
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -25,11 +25,8 @@
|
|||||||
|
|
||||||
package me.lucko.luckperms.common.contexts;
|
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.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;
|
||||||
@ -38,6 +35,7 @@ import me.lucko.luckperms.api.context.ContextCalculator;
|
|||||||
import me.lucko.luckperms.api.context.ImmutableContextSet;
|
import me.lucko.luckperms.api.context.ImmutableContextSet;
|
||||||
import me.lucko.luckperms.api.context.MutableContextSet;
|
import me.lucko.luckperms.api.context.MutableContextSet;
|
||||||
import me.lucko.luckperms.api.context.StaticContextCalculator;
|
import me.lucko.luckperms.api.context.StaticContextCalculator;
|
||||||
|
import me.lucko.luckperms.common.buffers.ExpiringCache;
|
||||||
import me.lucko.luckperms.common.config.ConfigKeys;
|
import me.lucko.luckperms.common.config.ConfigKeys;
|
||||||
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
||||||
|
|
||||||
@ -64,14 +62,14 @@ public abstract class AbstractContextManager<T> implements ContextManager<T> {
|
|||||||
private final List<ContextCalculator<? super T>> calculators = new CopyOnWriteArrayList<>();
|
private final List<ContextCalculator<? super T>> calculators = new CopyOnWriteArrayList<>();
|
||||||
private final List<StaticContextCalculator> staticCalculators = new CopyOnWriteArrayList<>();
|
private final List<StaticContextCalculator> staticCalculators = new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
// caches context lookups
|
// caches the creation of cache instances. cache-ception.
|
||||||
private final LoadingCache<T, Contexts> lookupCache = Caffeine.newBuilder()
|
// we want to encourage re-use of these instances, it's faster that way
|
||||||
.expireAfterWrite(50L, TimeUnit.MILLISECONDS) // expire roughly every tick
|
private final LoadingCache<T, ContextsCache<T>> subjectCaches = Caffeine.newBuilder()
|
||||||
.build(new Loader());
|
.weakKeys()
|
||||||
|
.build(key -> new ContextsCache<>(key, this));
|
||||||
|
|
||||||
// caches static context lookups
|
// caches static context lookups
|
||||||
@SuppressWarnings("Guava")
|
private final StaticLookupCache staticLookupCache = new StaticLookupCache();
|
||||||
private final Supplier<Contexts> staticLookupCache = Suppliers.memoizeWithExpiration(new StaticLoader(), 50L, TimeUnit.MILLISECONDS);
|
|
||||||
|
|
||||||
protected AbstractContextManager(LuckPermsPlugin plugin, Class<T> subjectClass) {
|
protected AbstractContextManager(LuckPermsPlugin plugin, Class<T> subjectClass) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
@ -95,21 +93,20 @@ public abstract class AbstractContextManager<T> implements ContextManager<T> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ImmutableContextSet getApplicableContext(T subject) {
|
public ImmutableContextSet getApplicableContext(T subject) {
|
||||||
if (subject == null) {
|
return getCacheFor(subject).getContextSet();
|
||||||
throw new NullPointerException("subject");
|
|
||||||
}
|
|
||||||
|
|
||||||
// this is actually already immutable, but the Contexts method signature returns the interface.
|
|
||||||
// using the makeImmutable method is faster than casting
|
|
||||||
return getApplicableContexts(subject).getContexts().makeImmutable();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Contexts getApplicableContexts(T subject) {
|
public Contexts getApplicableContexts(T subject) {
|
||||||
|
return getCacheFor(subject).getContexts();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ContextsCache<T> getCacheFor(T subject) {
|
||||||
if (subject == null) {
|
if (subject == null) {
|
||||||
throw new NullPointerException("subject");
|
throw new NullPointerException("subject");
|
||||||
}
|
}
|
||||||
return this.lookupCache.get(subject);
|
return this.subjectCaches.get(subject);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -181,52 +178,58 @@ public abstract class AbstractContextManager<T> implements ContextManager<T> {
|
|||||||
throw new NullPointerException("subject");
|
throw new NullPointerException("subject");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.lookupCache.invalidate(subject);
|
this.subjectCaches.invalidate(subject);
|
||||||
}
|
}
|
||||||
|
|
||||||
private final class Loader implements CacheLoader<T, Contexts> {
|
Contexts calculate(T subject) {
|
||||||
@Override
|
MutableContextSet accumulator = MutableContextSet.create();
|
||||||
public Contexts load(@Nonnull T subject) {
|
|
||||||
MutableContextSet accumulator = MutableContextSet.create();
|
|
||||||
|
|
||||||
for (ContextCalculator<? super T> calculator : AbstractContextManager.this.calculators) {
|
for (ContextCalculator<? super T> calculator : AbstractContextManager.this.calculators) {
|
||||||
try {
|
try {
|
||||||
MutableContextSet ret = calculator.giveApplicableContext(subject, accumulator);
|
MutableContextSet ret = calculator.giveApplicableContext(subject, accumulator);
|
||||||
//noinspection ConstantConditions
|
//noinspection ConstantConditions
|
||||||
if (ret == null) {
|
if (ret == null) {
|
||||||
throw new IllegalStateException(calculator.getClass() + " returned a null context set");
|
throw new IllegalStateException(calculator.getClass() + " returned a null context set");
|
||||||
}
|
|
||||||
accumulator = ret;
|
|
||||||
} catch (Exception e) {
|
|
||||||
AbstractContextManager.this.plugin.getLogger().warn("An exception was thrown by " + getCalculatorClass(calculator) + " whilst calculating the context of subject " + subject);
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
accumulator = ret;
|
||||||
|
} catch (Exception e) {
|
||||||
|
AbstractContextManager.this.plugin.getLogger().warn("An exception was thrown by " + getCalculatorClass(calculator) + " whilst calculating the context of subject " + subject);
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return formContexts(subject, accumulator.makeImmutable());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return formContexts(subject, accumulator.makeImmutable());
|
||||||
}
|
}
|
||||||
|
|
||||||
private final class StaticLoader implements Supplier<Contexts> {
|
private Contexts calculateStatic() {
|
||||||
@Override
|
MutableContextSet accumulator = MutableContextSet.create();
|
||||||
public Contexts get() {
|
|
||||||
MutableContextSet accumulator = MutableContextSet.create();
|
|
||||||
|
|
||||||
for (StaticContextCalculator calculator : AbstractContextManager.this.staticCalculators) {
|
for (StaticContextCalculator calculator : this.staticCalculators) {
|
||||||
try {
|
try {
|
||||||
MutableContextSet ret = calculator.giveApplicableContext(accumulator);
|
MutableContextSet ret = calculator.giveApplicableContext(accumulator);
|
||||||
//noinspection ConstantConditions
|
//noinspection ConstantConditions
|
||||||
if (ret == null) {
|
if (ret == null) {
|
||||||
throw new IllegalStateException(calculator.getClass() + " returned a null context set");
|
throw new IllegalStateException(calculator.getClass() + " returned a null context set");
|
||||||
}
|
|
||||||
accumulator = ret;
|
|
||||||
} catch (Exception e) {
|
|
||||||
AbstractContextManager.this.plugin.getLogger().warn("An exception was thrown by " + getCalculatorClass(calculator) + " whilst calculating static contexts");
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
accumulator = ret;
|
||||||
|
} catch (Exception e) {
|
||||||
|
this.plugin.getLogger().warn("An exception was thrown by " + getCalculatorClass(calculator) + " whilst calculating static contexts");
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return formContexts(accumulator.makeImmutable());
|
return formContexts(accumulator.makeImmutable());
|
||||||
|
}
|
||||||
|
|
||||||
|
private final class StaticLookupCache extends ExpiringCache<Contexts> {
|
||||||
|
StaticLookupCache() {
|
||||||
|
super(50L, TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
public Contexts supply() {
|
||||||
|
return calculateStatic();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,6 +65,14 @@ public interface ContextManager<T> {
|
|||||||
*/
|
*/
|
||||||
Contexts getApplicableContexts(T subject);
|
Contexts getApplicableContexts(T subject);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the cache instance for the given subject.
|
||||||
|
*
|
||||||
|
* @param subject the subject
|
||||||
|
* @return the cache
|
||||||
|
*/
|
||||||
|
ContextsCache<T> getCacheFor(T subject);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the contexts from the static calculators in this manager.
|
* Gets the contexts from the static calculators in this manager.
|
||||||
*
|
*
|
||||||
|
@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of LuckPerms, licensed under the MIT License.
|
||||||
|
*
|
||||||
|
* Copyright (c) lucko (Luck) <luck@lucko.me>
|
||||||
|
* Copyright (c) contributors
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package me.lucko.luckperms.common.contexts;
|
||||||
|
|
||||||
|
import me.lucko.luckperms.api.Contexts;
|
||||||
|
import me.lucko.luckperms.api.context.ImmutableContextSet;
|
||||||
|
import me.lucko.luckperms.common.buffers.ExpiringCache;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extension of {@link AbstractContextManager} that implements an expiring lookup cache
|
||||||
|
* per player.
|
||||||
|
*
|
||||||
|
* @param <T> the player type
|
||||||
|
*/
|
||||||
|
public final class ContextsCache<T> extends ExpiringCache<Contexts> {
|
||||||
|
private final T subject;
|
||||||
|
private final AbstractContextManager<T> contextManager;
|
||||||
|
|
||||||
|
public ContextsCache(T subject, AbstractContextManager<T> contextManager) {
|
||||||
|
super(50L, TimeUnit.MILLISECONDS); // expire roughly every tick
|
||||||
|
this.subject = subject;
|
||||||
|
this.contextManager = contextManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
@Override
|
||||||
|
protected Contexts supply() {
|
||||||
|
return this.contextManager.calculate(this.subject);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Contexts getContexts() {
|
||||||
|
return get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ImmutableContextSet getContextSet() {
|
||||||
|
// this is actually already immutable, but the Contexts method signature returns the interface.
|
||||||
|
// using the makeImmutable method is faster than casting
|
||||||
|
return get().getContexts().makeImmutable();
|
||||||
|
}
|
||||||
|
}
|
@ -25,9 +25,9 @@
|
|||||||
|
|
||||||
package me.lucko.luckperms.nukkit.model.permissible;
|
package me.lucko.luckperms.nukkit.model.permissible;
|
||||||
|
|
||||||
import me.lucko.luckperms.api.Contexts;
|
|
||||||
import me.lucko.luckperms.api.Tristate;
|
import me.lucko.luckperms.api.Tristate;
|
||||||
import me.lucko.luckperms.common.config.ConfigKeys;
|
import me.lucko.luckperms.common.config.ConfigKeys;
|
||||||
|
import me.lucko.luckperms.common.contexts.ContextsCache;
|
||||||
import me.lucko.luckperms.common.model.User;
|
import me.lucko.luckperms.common.model.User;
|
||||||
import me.lucko.luckperms.common.verbose.CheckOrigin;
|
import me.lucko.luckperms.common.verbose.CheckOrigin;
|
||||||
import me.lucko.luckperms.nukkit.LPNukkitPlugin;
|
import me.lucko.luckperms.nukkit.LPNukkitPlugin;
|
||||||
@ -73,6 +73,9 @@ public class LPPermissible extends PermissibleBase {
|
|||||||
// the luckperms plugin instance
|
// the luckperms plugin instance
|
||||||
private final LPNukkitPlugin plugin;
|
private final LPNukkitPlugin plugin;
|
||||||
|
|
||||||
|
// caches context lookups for the player
|
||||||
|
private final ContextsCache<Player> contextsCache;
|
||||||
|
|
||||||
// the players previous permissible. (the one they had before this one was injected)
|
// the players previous permissible. (the one they had before this one was injected)
|
||||||
private PermissibleBase oldPermissible = null;
|
private PermissibleBase oldPermissible = null;
|
||||||
|
|
||||||
@ -88,6 +91,7 @@ public class LPPermissible extends PermissibleBase {
|
|||||||
this.user = Objects.requireNonNull(user, "user");
|
this.user = Objects.requireNonNull(user, "user");
|
||||||
this.player = Objects.requireNonNull(player, "player");
|
this.player = Objects.requireNonNull(player, "player");
|
||||||
this.plugin = Objects.requireNonNull(plugin, "plugin");
|
this.plugin = Objects.requireNonNull(plugin, "plugin");
|
||||||
|
this.contextsCache = plugin.getContextManager().getCacheFor(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -96,7 +100,7 @@ public class LPPermissible extends PermissibleBase {
|
|||||||
throw new NullPointerException("permission");
|
throw new NullPointerException("permission");
|
||||||
}
|
}
|
||||||
|
|
||||||
Tristate ts = this.user.getCachedData().getPermissionData(calculateContexts()).getPermissionValue(permission, CheckOrigin.PLATFORM_LOOKUP_CHECK);
|
Tristate ts = this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getPermissionValue(permission, CheckOrigin.PLATFORM_LOOKUP_CHECK);
|
||||||
return ts != Tristate.UNDEFINED || PermissionDefault.OP.getValue(isOp());
|
return ts != Tristate.UNDEFINED || PermissionDefault.OP.getValue(isOp());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,7 +110,7 @@ public class LPPermissible extends PermissibleBase {
|
|||||||
throw new NullPointerException("permission");
|
throw new NullPointerException("permission");
|
||||||
}
|
}
|
||||||
|
|
||||||
Tristate ts = this.user.getCachedData().getPermissionData(calculateContexts()).getPermissionValue(permission.getName(), CheckOrigin.PLATFORM_LOOKUP_CHECK);
|
Tristate ts = this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getPermissionValue(permission.getName(), CheckOrigin.PLATFORM_LOOKUP_CHECK);
|
||||||
if (ts != Tristate.UNDEFINED) {
|
if (ts != Tristate.UNDEFINED) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -125,7 +129,7 @@ public class LPPermissible extends PermissibleBase {
|
|||||||
throw new NullPointerException("permission");
|
throw new NullPointerException("permission");
|
||||||
}
|
}
|
||||||
|
|
||||||
Tristate ts = this.user.getCachedData().getPermissionData(calculateContexts()).getPermissionValue(permission, CheckOrigin.PLATFORM_PERMISSION_CHECK);
|
Tristate ts = this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getPermissionValue(permission, CheckOrigin.PLATFORM_PERMISSION_CHECK);
|
||||||
return ts != Tristate.UNDEFINED ? ts.asBoolean() : PermissionDefault.OP.getValue(isOp());
|
return ts != Tristate.UNDEFINED ? ts.asBoolean() : PermissionDefault.OP.getValue(isOp());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,7 +139,7 @@ public class LPPermissible extends PermissibleBase {
|
|||||||
throw new NullPointerException("permission");
|
throw new NullPointerException("permission");
|
||||||
}
|
}
|
||||||
|
|
||||||
Tristate ts = this.user.getCachedData().getPermissionData(calculateContexts()).getPermissionValue(permission.getName(), CheckOrigin.PLATFORM_PERMISSION_CHECK);
|
Tristate ts = this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getPermissionValue(permission.getName(), CheckOrigin.PLATFORM_PERMISSION_CHECK);
|
||||||
if (ts != Tristate.UNDEFINED) {
|
if (ts != Tristate.UNDEFINED) {
|
||||||
return ts.asBoolean();
|
return ts.asBoolean();
|
||||||
}
|
}
|
||||||
@ -159,16 +163,6 @@ public class LPPermissible extends PermissibleBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Obtains a {@link Contexts} instance for the player.
|
|
||||||
* Values are determined using the plugins ContextManager.
|
|
||||||
*
|
|
||||||
* @return the calculated contexts for the player.
|
|
||||||
*/
|
|
||||||
private Contexts calculateContexts() {
|
|
||||||
return this.plugin.getContextManager().getApplicableContexts(this.player);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setOp(boolean value) {
|
public void setOp(boolean value) {
|
||||||
this.player.setOp(value);
|
this.player.setOp(value);
|
||||||
@ -176,7 +170,7 @@ public class LPPermissible extends PermissibleBase {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, PermissionAttachmentInfo> getEffectivePermissions() {
|
public Map<String, PermissionAttachmentInfo> getEffectivePermissions() {
|
||||||
Set<Map.Entry<String, Boolean>> permissions = this.user.getCachedData().getPermissionData(calculateContexts()).getImmutableBacking().entrySet();
|
Set<Map.Entry<String, Boolean>> permissions = this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getImmutableBacking().entrySet();
|
||||||
Map<String, PermissionAttachmentInfo> ret = new HashMap<>(permissions.size());
|
Map<String, PermissionAttachmentInfo> ret = new HashMap<>(permissions.size());
|
||||||
|
|
||||||
for (Map.Entry<String, Boolean> entry : permissions) {
|
for (Map.Entry<String, Boolean> entry : permissions) {
|
||||||
|
@ -26,6 +26,8 @@
|
|||||||
package me.lucko.luckperms.sponge.service.proxy.api6;
|
package me.lucko.luckperms.sponge.service.proxy.api6;
|
||||||
|
|
||||||
import me.lucko.luckperms.api.context.ImmutableContextSet;
|
import me.lucko.luckperms.api.context.ImmutableContextSet;
|
||||||
|
import me.lucko.luckperms.common.contexts.ContextManager;
|
||||||
|
import me.lucko.luckperms.common.contexts.ContextsCache;
|
||||||
import me.lucko.luckperms.common.utils.ImmutableCollectors;
|
import me.lucko.luckperms.common.utils.ImmutableCollectors;
|
||||||
import me.lucko.luckperms.sponge.service.CompatibilityUtil;
|
import me.lucko.luckperms.sponge.service.CompatibilityUtil;
|
||||||
import me.lucko.luckperms.sponge.service.model.LPPermissionService;
|
import me.lucko.luckperms.sponge.service.model.LPPermissionService;
|
||||||
@ -52,9 +54,14 @@ public final class SubjectProxy implements Subject, ProxiedSubject {
|
|||||||
private final LPPermissionService service;
|
private final LPPermissionService service;
|
||||||
private final LPSubjectReference ref;
|
private final LPSubjectReference ref;
|
||||||
|
|
||||||
|
private final ContextsCache<Subject> contextsCache;
|
||||||
|
|
||||||
public SubjectProxy(LPPermissionService service, LPSubjectReference ref) {
|
public SubjectProxy(LPPermissionService service, LPSubjectReference ref) {
|
||||||
this.service = service;
|
this.service = service;
|
||||||
this.ref = ref;
|
this.ref = ref;
|
||||||
|
|
||||||
|
ContextManager<Subject> contextManager = (ContextManager<Subject>) service.getPlugin().getContextManager();
|
||||||
|
this.contextsCache = contextManager.getCacheFor(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private CompletableFuture<LPSubject> handle() {
|
private CompletableFuture<LPSubject> handle() {
|
||||||
@ -157,7 +164,7 @@ public final class SubjectProxy implements Subject, ProxiedSubject {
|
|||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public Set<Context> getActiveContexts() {
|
public Set<Context> getActiveContexts() {
|
||||||
return CompatibilityUtil.convertContexts(this.service.getContextManager().getApplicableContext(this));
|
return CompatibilityUtil.convertContexts(this.contextsCache.getContextSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -26,6 +26,8 @@
|
|||||||
package me.lucko.luckperms.sponge.service.proxy.api7;
|
package me.lucko.luckperms.sponge.service.proxy.api7;
|
||||||
|
|
||||||
import me.lucko.luckperms.api.context.ImmutableContextSet;
|
import me.lucko.luckperms.api.context.ImmutableContextSet;
|
||||||
|
import me.lucko.luckperms.common.contexts.ContextManager;
|
||||||
|
import me.lucko.luckperms.common.contexts.ContextsCache;
|
||||||
import me.lucko.luckperms.sponge.service.CompatibilityUtil;
|
import me.lucko.luckperms.sponge.service.CompatibilityUtil;
|
||||||
import me.lucko.luckperms.sponge.service.model.LPPermissionService;
|
import me.lucko.luckperms.sponge.service.model.LPPermissionService;
|
||||||
import me.lucko.luckperms.sponge.service.model.LPSubject;
|
import me.lucko.luckperms.sponge.service.model.LPSubject;
|
||||||
@ -52,9 +54,14 @@ public final class SubjectProxy implements Subject, ProxiedSubject {
|
|||||||
private final LPPermissionService service;
|
private final LPPermissionService service;
|
||||||
private final LPSubjectReference ref;
|
private final LPSubjectReference ref;
|
||||||
|
|
||||||
|
private final ContextsCache<Subject> contextsCache;
|
||||||
|
|
||||||
public SubjectProxy(LPPermissionService service, LPSubjectReference ref) {
|
public SubjectProxy(LPPermissionService service, LPSubjectReference ref) {
|
||||||
this.service = service;
|
this.service = service;
|
||||||
this.ref = ref;
|
this.ref = ref;
|
||||||
|
|
||||||
|
ContextManager<Subject> contextManager = (ContextManager<Subject>) service.getPlugin().getContextManager();
|
||||||
|
this.contextsCache = contextManager.getCacheFor(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private CompletableFuture<LPSubject> handle() {
|
private CompletableFuture<LPSubject> handle() {
|
||||||
@ -158,7 +165,7 @@ public final class SubjectProxy implements Subject, ProxiedSubject {
|
|||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public Set<Context> getActiveContexts() {
|
public Set<Context> getActiveContexts() {
|
||||||
return CompatibilityUtil.convertContexts(this.service.getContextManager().getApplicableContext(this));
|
return CompatibilityUtil.convertContexts(this.contextsCache.getContextSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user