Refactor the way contexts are cached on all platforms (#1071)

Should fix memory leak issues on BungeeCord
This commit is contained in:
Luck 2018-06-27 00:17:53 +01:00
parent ee13540d78
commit 08454d58d0
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
15 changed files with 285 additions and 62 deletions

View File

@ -50,7 +50,6 @@ import me.lucko.luckperms.common.calculators.CalculatorFactory;
import me.lucko.luckperms.common.command.access.CommandPermission; import me.lucko.luckperms.common.command.access.CommandPermission;
import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter;
import me.lucko.luckperms.common.contexts.ContextManager;
import me.lucko.luckperms.common.dependencies.Dependency; import me.lucko.luckperms.common.dependencies.Dependency;
import me.lucko.luckperms.common.event.AbstractEventBus; import me.lucko.luckperms.common.event.AbstractEventBus;
import me.lucko.luckperms.common.managers.group.StandardGroupManager; import me.lucko.luckperms.common.managers.group.StandardGroupManager;
@ -93,7 +92,7 @@ public class LPBukkitPlugin extends AbstractLuckPermsPlugin {
private StandardUserManager userManager; private StandardUserManager userManager;
private StandardGroupManager groupManager; private StandardGroupManager groupManager;
private StandardTrackManager trackManager; private StandardTrackManager trackManager;
private ContextManager<Player> contextManager; private BukkitContextManager contextManager;
private LPSubscriptionMap subscriptionMap; private LPSubscriptionMap subscriptionMap;
private LPPermissionMap permissionMap; private LPPermissionMap permissionMap;
private LPDefaultsMap defaultPermissionMap; private LPDefaultsMap defaultPermissionMap;
@ -375,7 +374,7 @@ public class LPBukkitPlugin extends AbstractLuckPermsPlugin {
} }
@Override @Override
public ContextManager<Player> getContextManager() { public BukkitContextManager getContextManager() {
return this.contextManager; return this.contextManager;
} }

View File

@ -25,22 +25,78 @@
package me.lucko.luckperms.bukkit.contexts; package me.lucko.luckperms.bukkit.contexts;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.LookupSetting; import me.lucko.luckperms.api.LookupSetting;
import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.api.context.ImmutableContextSet;
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.ContextManager; import me.lucko.luckperms.common.contexts.ContextManager;
import me.lucko.luckperms.common.contexts.ContextsCache;
import me.lucko.luckperms.common.contexts.ContextsSupplier;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.concurrent.TimeUnit;
public class BukkitContextManager extends ContextManager<Player> { public class BukkitContextManager extends ContextManager<Player> {
// cache the creation of ContextsCache instances for online players with no expiry
private final LoadingCache<Player, ContextsCache<Player>> onlineSubjectCaches = Caffeine.newBuilder()
.build(key -> new ContextsCache<>(key, this));
// cache the creation of ContextsCache instances for offline players with a 1m expiry
private final LoadingCache<Player, ContextsCache<Player>> offlineSubjectCaches = Caffeine.newBuilder()
.expireAfterAccess(1, TimeUnit.MINUTES)
.build(key -> {
ContextsCache<Player> cache = this.onlineSubjectCaches.getIfPresent(key);
if (cache != null) {
return cache;
}
return new ContextsCache<>(key, this);
});
public BukkitContextManager(LPBukkitPlugin plugin) { public BukkitContextManager(LPBukkitPlugin plugin) {
super(plugin, Player.class); super(plugin, Player.class);
} }
public void onPlayerQuit(Player player) {
this.onlineSubjectCaches.invalidate(player);
}
@Override
public ContextsSupplier getCacheFor(Player subject) {
if (subject == null) {
throw new NullPointerException("subject");
}
if (subject.isOnline()) {
return this.onlineSubjectCaches.get(subject);
} else {
return this.offlineSubjectCaches.get(subject);
}
}
@Override
public void invalidateCache(Player subject) {
if (subject == null) {
throw new NullPointerException("subject");
}
ContextsCache<Player> cache = this.onlineSubjectCaches.getIfPresent(subject);
if (cache != null) {
cache.invalidate();
}
cache = this.offlineSubjectCaches.getIfPresent(subject);
if (cache != null) {
cache.invalidate();
}
}
@Override @Override
public Contexts formContexts(Player subject, ImmutableContextSet contextSet) { public Contexts formContexts(Player subject, ImmutableContextSet contextSet) {
EnumSet<LookupSetting> settings = this.plugin.getConfiguration().get(ConfigKeys.LOOKUP_SETTINGS); EnumSet<LookupSetting> settings = this.plugin.getConfiguration().get(ConfigKeys.LOOKUP_SETTINGS);

View File

@ -197,6 +197,9 @@ public class BukkitConnectionListener extends AbstractConnectionListener impleme
user.clearTransientNodes(); user.clearTransientNodes();
} }
}); });
// remove their contexts cache
this.plugin.getContextManager().onPlayerQuit(player);
} }
} }

View File

@ -30,7 +30,7 @@ import com.google.common.collect.ImmutableList;
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.contexts.ContextsSupplier;
import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.utils.ImmutableCollectors; import me.lucko.luckperms.common.utils.ImmutableCollectors;
import me.lucko.luckperms.common.verbose.CheckOrigin; import me.lucko.luckperms.common.verbose.CheckOrigin;
@ -91,7 +91,7 @@ public class LPPermissible extends PermissibleBase {
private final LPBukkitPlugin plugin; private final LPBukkitPlugin plugin;
// caches context lookups for the player // caches context lookups for the player
private final ContextsCache<Player> contextsCache; private final ContextsSupplier contextsSupplier;
// 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;
@ -108,7 +108,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); this.contextsSupplier = plugin.getContextManager().getCacheFor(player);
injectFakeAttachmentsList(); injectFakeAttachmentsList();
} }
@ -137,7 +137,7 @@ public class LPPermissible extends PermissibleBase {
throw new NullPointerException("permission"); throw new NullPointerException("permission");
} }
Tristate ts = this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getPermissionValue(permission, CheckOrigin.PLATFORM_LOOKUP_CHECK); Tristate ts = this.user.getCachedData().getPermissionData(this.contextsSupplier.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());
} }
@ -147,7 +147,7 @@ public class LPPermissible extends PermissibleBase {
throw new NullPointerException("permission"); throw new NullPointerException("permission");
} }
Tristate ts = this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getPermissionValue(permission.getName(), CheckOrigin.PLATFORM_LOOKUP_CHECK); Tristate ts = this.user.getCachedData().getPermissionData(this.contextsSupplier.getContexts()).getPermissionValue(permission.getName(), CheckOrigin.PLATFORM_LOOKUP_CHECK);
if (ts != Tristate.UNDEFINED) { if (ts != Tristate.UNDEFINED) {
return true; return true;
} }
@ -165,7 +165,7 @@ public class LPPermissible extends PermissibleBase {
throw new NullPointerException("permission"); throw new NullPointerException("permission");
} }
Tristate ts = this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getPermissionValue(permission, CheckOrigin.PLATFORM_PERMISSION_CHECK); Tristate ts = this.user.getCachedData().getPermissionData(this.contextsSupplier.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());
} }
@ -175,7 +175,7 @@ public class LPPermissible extends PermissibleBase {
throw new NullPointerException("permission"); throw new NullPointerException("permission");
} }
Tristate ts = this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getPermissionValue(permission.getName(), CheckOrigin.PLATFORM_PERMISSION_CHECK); Tristate ts = this.user.getCachedData().getPermissionData(this.contextsSupplier.getContexts()).getPermissionValue(permission.getName(), CheckOrigin.PLATFORM_PERMISSION_CHECK);
if (ts != Tristate.UNDEFINED) { if (ts != Tristate.UNDEFINED) {
return ts.asBoolean(); return ts.asBoolean();
} }
@ -205,7 +205,7 @@ public class LPPermissible extends PermissibleBase {
@Override @Override
public Set<PermissionAttachmentInfo> getEffectivePermissions() { public Set<PermissionAttachmentInfo> getEffectivePermissions() {
return this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getImmutableBacking().entrySet().stream() return this.user.getCachedData().getPermissionData(this.contextsSupplier.getContexts()).getImmutableBacking().entrySet().stream()
.map(entry -> new PermissionAttachmentInfo(this.player, entry.getKey(), null, entry.getValue())) .map(entry -> new PermissionAttachmentInfo(this.player, entry.getKey(), null, entry.getValue()))
.collect(ImmutableCollectors.toSet()); .collect(ImmutableCollectors.toSet());
} }

View File

@ -25,20 +25,75 @@
package me.lucko.luckperms.bungee.contexts; package me.lucko.luckperms.bungee.contexts;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.bungee.LPBungeePlugin; import me.lucko.luckperms.bungee.LPBungeePlugin;
import me.lucko.luckperms.common.contexts.ContextManager; import me.lucko.luckperms.common.contexts.ContextManager;
import me.lucko.luckperms.common.contexts.ContextsSupplier;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.util.concurrent.TimeUnit;
public class BungeeContextManager extends ContextManager<ProxiedPlayer> { public class BungeeContextManager extends ContextManager<ProxiedPlayer> {
private final LoadingCache<ProxiedPlayer, Contexts> contextsCache = Caffeine.newBuilder()
.expireAfterWrite(50, TimeUnit.MILLISECONDS)
.build(this::calculate);
public BungeeContextManager(LPBungeePlugin plugin) { public BungeeContextManager(LPBungeePlugin plugin) {
super(plugin, ProxiedPlayer.class); super(plugin, ProxiedPlayer.class);
} }
@Override
public ContextsSupplier getCacheFor(ProxiedPlayer subject) {
if (subject == null) {
throw new NullPointerException("subject");
}
return new InlineContextsSupplier(subject, this.contextsCache);
}
@Override
public ImmutableContextSet getApplicableContext(ProxiedPlayer subject) {
return getApplicableContexts(subject).getContexts().makeImmutable();
}
@Override
public Contexts getApplicableContexts(ProxiedPlayer subject) {
return this.contextsCache.get(subject);
}
@Override
public void invalidateCache(ProxiedPlayer subject) {
this.contextsCache.invalidate(subject);
}
@Override @Override
public Contexts formContexts(ProxiedPlayer subject, ImmutableContextSet contextSet) { public Contexts formContexts(ProxiedPlayer subject, ImmutableContextSet contextSet) {
return formContexts(contextSet); return formContexts(contextSet);
} }
private static final class InlineContextsSupplier implements ContextsSupplier {
private final ProxiedPlayer key;
private final LoadingCache<ProxiedPlayer, Contexts> contextsCache;
private InlineContextsSupplier(ProxiedPlayer key, LoadingCache<ProxiedPlayer, Contexts> contextsCache) {
this.key = key;
this.contextsCache = contextsCache;
}
@Override
public Contexts getContexts() {
return this.contextsCache.get(this.key);
}
@Override
public ImmutableContextSet getContextSet() {
return getContexts().getContexts().makeImmutable();
}
}
} }

View File

@ -25,8 +25,6 @@
package me.lucko.luckperms.common.contexts; package me.lucko.luckperms.common.contexts;
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.ImmutableList;
import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Contexts;
@ -62,12 +60,6 @@ public abstract class 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 the creation of cache instances. cache-ception.
// we want to encourage re-use of these instances, it's faster that way
private final LoadingCache<T, ContextsCache<T>> subjectCaches = Caffeine.newBuilder()
.weakKeys()
.build(key -> new ContextsCache<>(key, this));
// caches static context lookups // caches static context lookups
private final StaticLookupCache staticLookupCache = new StaticLookupCache(); private final StaticLookupCache staticLookupCache = new StaticLookupCache();
@ -129,12 +121,7 @@ public abstract class ContextManager<T> {
* @param subject the subject * @param subject the subject
* @return the cache * @return the cache
*/ */
public ContextsCache<T> getCacheFor(T subject) { public abstract ContextsSupplier getCacheFor(T subject);
if (subject == null) {
throw new NullPointerException("subject");
}
return this.subjectCaches.get(subject);
}
/** /**
* Gets the contexts from the static calculators in this manager. * Gets the contexts from the static calculators in this manager.
@ -228,18 +215,9 @@ public abstract class ContextManager<T> {
* *
* @param subject the subject * @param subject the subject
*/ */
public void invalidateCache(T subject) { public abstract void invalidateCache(T subject);
if (subject == null) {
throw new NullPointerException("subject");
}
ContextsCache<T> cache = this.subjectCaches.getIfPresent(subject); protected Contexts calculate(T subject) {
if (cache != null) {
cache.invalidate();
}
}
Contexts calculate(T subject) {
MutableContextSet accumulator = MutableContextSet.create(); MutableContextSet accumulator = MutableContextSet.create();
for (ContextCalculator<? super T> calculator : ContextManager.this.calculators) { for (ContextCalculator<? super T> calculator : ContextManager.this.calculators) {

View File

@ -34,12 +34,11 @@ import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
/** /**
* Extension of {@link ContextManager} that implements an expiring lookup cache * Implementation of {@link ContextsSupplier} that caches results.
* per player.
* *
* @param <T> the player type * @param <T> the player type
*/ */
public final class ContextsCache<T> extends ExpiringCache<Contexts> { public final class ContextsCache<T> extends ExpiringCache<Contexts> implements ContextsSupplier {
private final T subject; private final T subject;
private final ContextManager<T> contextManager; private final ContextManager<T> contextManager;
@ -55,10 +54,12 @@ public final class ContextsCache<T> extends ExpiringCache<Contexts> {
return this.contextManager.calculate(this.subject); return this.contextManager.calculate(this.subject);
} }
@Override
public Contexts getContexts() { public Contexts getContexts() {
return get(); return get();
} }
@Override
public ImmutableContextSet getContextSet() { public ImmutableContextSet getContextSet() {
// this is actually already immutable, but the Contexts method signature returns the interface. // this is actually already immutable, but the Contexts method signature returns the interface.
// using the makeImmutable method is faster than casting // using the makeImmutable method is faster than casting

View File

@ -0,0 +1,40 @@
/*
* 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;
/**
* Supplies contexts for a given subject.
*/
public interface ContextsSupplier {
Contexts getContexts();
ImmutableContextSet getContextSet();
}

View File

@ -34,7 +34,6 @@ import me.lucko.luckperms.common.calculators.CalculatorFactory;
import me.lucko.luckperms.common.command.access.CommandPermission; import me.lucko.luckperms.common.command.access.CommandPermission;
import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter; import me.lucko.luckperms.common.config.adapter.ConfigurationAdapter;
import me.lucko.luckperms.common.contexts.ContextManager;
import me.lucko.luckperms.common.dependencies.Dependency; import me.lucko.luckperms.common.dependencies.Dependency;
import me.lucko.luckperms.common.event.AbstractEventBus; import me.lucko.luckperms.common.event.AbstractEventBus;
import me.lucko.luckperms.common.managers.group.StandardGroupManager; import me.lucko.luckperms.common.managers.group.StandardGroupManager;
@ -90,7 +89,7 @@ public class LPNukkitPlugin extends AbstractLuckPermsPlugin {
private StandardUserManager userManager; private StandardUserManager userManager;
private StandardGroupManager groupManager; private StandardGroupManager groupManager;
private StandardTrackManager trackManager; private StandardTrackManager trackManager;
private ContextManager<Player> contextManager; private NukkitContextManager contextManager;
private LPSubscriptionMap subscriptionMap; private LPSubscriptionMap subscriptionMap;
private LPPermissionMap permissionMap; private LPPermissionMap permissionMap;
private LPDefaultsMap defaultPermissionMap; private LPDefaultsMap defaultPermissionMap;
@ -339,7 +338,7 @@ public class LPNukkitPlugin extends AbstractLuckPermsPlugin {
} }
@Override @Override
public ContextManager<Player> getContextManager() { public NukkitContextManager getContextManager() {
return this.contextManager; return this.contextManager;
} }

View File

@ -25,22 +25,78 @@
package me.lucko.luckperms.nukkit.contexts; package me.lucko.luckperms.nukkit.contexts;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.LookupSetting; import me.lucko.luckperms.api.LookupSetting;
import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.contexts.ContextManager; import me.lucko.luckperms.common.contexts.ContextManager;
import me.lucko.luckperms.common.contexts.ContextsCache;
import me.lucko.luckperms.common.contexts.ContextsSupplier;
import me.lucko.luckperms.nukkit.LPNukkitPlugin; import me.lucko.luckperms.nukkit.LPNukkitPlugin;
import cn.nukkit.Player; import cn.nukkit.Player;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.concurrent.TimeUnit;
public class NukkitContextManager extends ContextManager<Player> { public class NukkitContextManager extends ContextManager<Player> {
// cache the creation of ContextsCache instances for online players with no expiry
private final LoadingCache<Player, ContextsCache<Player>> onlineSubjectCaches = Caffeine.newBuilder()
.build(key -> new ContextsCache<>(key, this));
// cache the creation of ContextsCache instances for offline players with a 1m expiry
private final LoadingCache<Player, ContextsCache<Player>> offlineSubjectCaches = Caffeine.newBuilder()
.expireAfterAccess(1, TimeUnit.MINUTES)
.build(key -> {
ContextsCache<Player> cache = this.onlineSubjectCaches.getIfPresent(key);
if (cache != null) {
return cache;
}
return new ContextsCache<>(key, this);
});
public NukkitContextManager(LPNukkitPlugin plugin) { public NukkitContextManager(LPNukkitPlugin plugin) {
super(plugin, Player.class); super(plugin, Player.class);
} }
public void onPlayerQuit(Player player) {
this.onlineSubjectCaches.invalidate(player);
}
@Override
public ContextsSupplier getCacheFor(Player subject) {
if (subject == null) {
throw new NullPointerException("subject");
}
if (subject.isOnline()) {
return this.onlineSubjectCaches.get(subject);
} else {
return this.offlineSubjectCaches.get(subject);
}
}
@Override
public void invalidateCache(Player subject) {
if (subject == null) {
throw new NullPointerException("subject");
}
ContextsCache<Player> cache = this.onlineSubjectCaches.getIfPresent(subject);
if (cache != null) {
cache.invalidate();
}
cache = this.offlineSubjectCaches.getIfPresent(subject);
if (cache != null) {
cache.invalidate();
}
}
@Override @Override
public Contexts formContexts(Player subject, ImmutableContextSet contextSet) { public Contexts formContexts(Player subject, ImmutableContextSet contextSet) {
EnumSet<LookupSetting> settings = this.plugin.getConfiguration().get(ConfigKeys.LOOKUP_SETTINGS); EnumSet<LookupSetting> settings = this.plugin.getConfiguration().get(ConfigKeys.LOOKUP_SETTINGS);

View File

@ -189,6 +189,9 @@ public class NukkitConnectionListener extends AbstractConnectionListener impleme
user.clearTransientNodes(); user.clearTransientNodes();
} }
}); });
// remove their contexts cache
this.plugin.getContextManager().onPlayerQuit(player);
} }
} }

View File

@ -29,7 +29,7 @@ import com.google.common.collect.ImmutableList;
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.contexts.ContextsSupplier;
import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.utils.ImmutableCollectors; import me.lucko.luckperms.common.utils.ImmutableCollectors;
import me.lucko.luckperms.common.verbose.CheckOrigin; import me.lucko.luckperms.common.verbose.CheckOrigin;
@ -91,7 +91,7 @@ public class LPPermissible extends PermissibleBase {
private final LPNukkitPlugin plugin; private final LPNukkitPlugin plugin;
// caches context lookups for the player // caches context lookups for the player
private final ContextsCache<Player> contextsCache; private final ContextsSupplier contextsSupplier;
// 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;
@ -108,7 +108,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); this.contextsSupplier = plugin.getContextManager().getCacheFor(player);
injectFakeAttachmentsList(); injectFakeAttachmentsList();
} }
@ -137,7 +137,7 @@ public class LPPermissible extends PermissibleBase {
throw new NullPointerException("permission"); throw new NullPointerException("permission");
} }
Tristate ts = this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getPermissionValue(permission, CheckOrigin.PLATFORM_LOOKUP_CHECK); Tristate ts = this.user.getCachedData().getPermissionData(this.contextsSupplier.getContexts()).getPermissionValue(permission, CheckOrigin.PLATFORM_LOOKUP_CHECK);
return ts != Tristate.UNDEFINED || PermissionDefault.OP.getValue(isOp()); return ts != Tristate.UNDEFINED || PermissionDefault.OP.getValue(isOp());
} }
@ -147,7 +147,7 @@ public class LPPermissible extends PermissibleBase {
throw new NullPointerException("permission"); throw new NullPointerException("permission");
} }
Tristate ts = this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getPermissionValue(permission.getName(), CheckOrigin.PLATFORM_LOOKUP_CHECK); Tristate ts = this.user.getCachedData().getPermissionData(this.contextsSupplier.getContexts()).getPermissionValue(permission.getName(), CheckOrigin.PLATFORM_LOOKUP_CHECK);
if (ts != Tristate.UNDEFINED) { if (ts != Tristate.UNDEFINED) {
return true; return true;
} }
@ -166,7 +166,7 @@ public class LPPermissible extends PermissibleBase {
throw new NullPointerException("permission"); throw new NullPointerException("permission");
} }
Tristate ts = this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getPermissionValue(permission, CheckOrigin.PLATFORM_PERMISSION_CHECK); Tristate ts = this.user.getCachedData().getPermissionData(this.contextsSupplier.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());
} }
@ -176,7 +176,7 @@ public class LPPermissible extends PermissibleBase {
throw new NullPointerException("permission"); throw new NullPointerException("permission");
} }
Tristate ts = this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getPermissionValue(permission.getName(), CheckOrigin.PLATFORM_PERMISSION_CHECK); Tristate ts = this.user.getCachedData().getPermissionData(this.contextsSupplier.getContexts()).getPermissionValue(permission.getName(), CheckOrigin.PLATFORM_PERMISSION_CHECK);
if (ts != Tristate.UNDEFINED) { if (ts != Tristate.UNDEFINED) {
return ts.asBoolean(); return ts.asBoolean();
} }
@ -207,7 +207,7 @@ public class LPPermissible extends PermissibleBase {
@Override @Override
public Map<String, PermissionAttachmentInfo> getEffectivePermissions() { public Map<String, PermissionAttachmentInfo> getEffectivePermissions() {
return this.user.getCachedData().getPermissionData(this.contextsCache.getContexts()).getImmutableBacking().entrySet().stream() return this.user.getCachedData().getPermissionData(this.contextsSupplier.getContexts()).getImmutableBacking().entrySet().stream()
.collect(ImmutableCollectors.toMap(Map.Entry::getKey, entry -> new PermissionAttachmentInfo(this.player, entry.getKey(), null, entry.getValue()))); .collect(ImmutableCollectors.toMap(Map.Entry::getKey, entry -> new PermissionAttachmentInfo(this.player, entry.getKey(), null, entry.getValue())));
} }

View File

@ -27,7 +27,7 @@ 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.ContextManager;
import me.lucko.luckperms.common.contexts.ContextsCache; import me.lucko.luckperms.common.contexts.ContextsSupplier;
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;
@ -54,7 +54,7 @@ public final class SubjectProxy implements Subject, ProxiedSubject {
private final LPPermissionService service; private final LPPermissionService service;
private final LPSubjectReference ref; private final LPSubjectReference ref;
private ContextsCache<Subject> contextsCache = null; private ContextsSupplier contextsSupplier = null;
public SubjectProxy(LPPermissionService service, LPSubjectReference ref) { public SubjectProxy(LPPermissionService service, LPSubjectReference ref) {
this.service = service; this.service = service;
@ -66,12 +66,12 @@ public final class SubjectProxy implements Subject, ProxiedSubject {
} }
// lazy init // lazy init
private ContextsCache<Subject> getContextsCache() { private ContextsSupplier getContextsCache() {
if (this.contextsCache == null) { if (this.contextsSupplier == null) {
ContextManager<Subject> contextManager = (ContextManager<Subject>) this.service.getPlugin().getContextManager(); ContextManager<Subject> contextManager = (ContextManager<Subject>) this.service.getPlugin().getContextManager();
this.contextsCache = contextManager.getCacheFor(this); this.contextsSupplier = contextManager.getCacheFor(this);
} }
return this.contextsCache; return this.contextsSupplier;
} }
@Nonnull @Nonnull

View File

@ -27,7 +27,7 @@ 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.ContextManager;
import me.lucko.luckperms.common.contexts.ContextsCache; import me.lucko.luckperms.common.contexts.ContextsSupplier;
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;
@ -54,7 +54,7 @@ public final class SubjectProxy implements Subject, ProxiedSubject {
private final LPPermissionService service; private final LPPermissionService service;
private final LPSubjectReference ref; private final LPSubjectReference ref;
private ContextsCache<Subject> contextsCache; private ContextsSupplier contextsSupplier;
public SubjectProxy(LPPermissionService service, LPSubjectReference ref) { public SubjectProxy(LPPermissionService service, LPSubjectReference ref) {
this.service = service; this.service = service;
@ -66,12 +66,12 @@ public final class SubjectProxy implements Subject, ProxiedSubject {
} }
// lazy init // lazy init
private ContextsCache<Subject> getContextsCache() { private ContextsSupplier getContextsCache() {
if (this.contextsCache == null) { if (this.contextsSupplier == null) {
ContextManager<Subject> contextManager = (ContextManager<Subject>) this.service.getPlugin().getContextManager(); ContextManager<Subject> contextManager = (ContextManager<Subject>) this.service.getPlugin().getContextManager();
this.contextsCache = contextManager.getCacheFor(this); this.contextsSupplier = contextManager.getCacheFor(this);
} }
return this.contextsCache; return this.contextsSupplier;
} }
@Nonnull @Nonnull

View File

@ -25,18 +25,51 @@
package me.lucko.luckperms.sponge.contexts; package me.lucko.luckperms.sponge.contexts;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.Contexts;
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.ContextManager;
import me.lucko.luckperms.common.contexts.ContextsCache;
import me.lucko.luckperms.common.contexts.ContextsSupplier;
import me.lucko.luckperms.sponge.LPSpongePlugin; import me.lucko.luckperms.sponge.LPSpongePlugin;
import org.spongepowered.api.service.permission.Subject; import org.spongepowered.api.service.permission.Subject;
import java.util.concurrent.TimeUnit;
public class SpongeContextManager extends ContextManager<Subject> { public class SpongeContextManager extends ContextManager<Subject> {
private final LoadingCache<Subject, ContextsCache<Subject>> subjectCaches = Caffeine.newBuilder()
.expireAfterAccess(1, TimeUnit.MINUTES)
.build(key -> new ContextsCache<>(key, this));
public SpongeContextManager(LPSpongePlugin plugin) { public SpongeContextManager(LPSpongePlugin plugin) {
super(plugin, Subject.class); super(plugin, Subject.class);
} }
@Override
public ContextsSupplier getCacheFor(Subject subject) {
if (subject == null) {
throw new NullPointerException("subject");
}
return subjectCaches.get(subject);
}
@Override
public void invalidateCache(Subject subject) {
if (subject == null) {
throw new NullPointerException("subject");
}
ContextsCache<Subject> cache = this.subjectCaches.getIfPresent(subject);
if (cache != null) {
cache.invalidate();
}
}
@Override @Override
public Contexts formContexts(Subject subject, ImmutableContextSet contextSet) { public Contexts formContexts(Subject subject, ImmutableContextSet contextSet) {
return formContexts(contextSet); return formContexts(contextSet);