Always load user data caches alongside user instances
This commit is contained in:
@@ -97,12 +97,13 @@ public final class UserDelegate extends PermissionHolderDelegate implements User
|
||||
|
||||
@Override
|
||||
public Optional<UserData> getUserDataCache() {
|
||||
return Optional.ofNullable(handle.getUserData());
|
||||
// TODO Deprecate this and return a nonnull instance
|
||||
return Optional.of(handle.getUserData());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setupDataCache() {
|
||||
handle.setupData(false);
|
||||
handle.preCalculateData(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -37,7 +37,6 @@ import me.lucko.luckperms.api.Contexts;
|
||||
import me.lucko.luckperms.api.caching.MetaData;
|
||||
import me.lucko.luckperms.api.caching.PermissionData;
|
||||
import me.lucko.luckperms.api.caching.UserData;
|
||||
import me.lucko.luckperms.common.calculators.CalculatorFactory;
|
||||
import me.lucko.luckperms.common.core.model.User;
|
||||
import me.lucko.luckperms.common.utils.ExtractedContexts;
|
||||
|
||||
@@ -50,17 +49,11 @@ import java.util.concurrent.TimeUnit;
|
||||
@RequiredArgsConstructor
|
||||
public class UserCache implements UserData {
|
||||
|
||||
|
||||
/**
|
||||
* The user whom this data instance is representing
|
||||
*/
|
||||
private final User user;
|
||||
|
||||
/**
|
||||
* A provider of {@link me.lucko.luckperms.common.calculators.PermissionCalculator}s for the instance
|
||||
*/
|
||||
private final CalculatorFactory calculatorFactory;
|
||||
|
||||
private final LoadingCache<Contexts, PermissionCache> permission = Caffeine.newBuilder()
|
||||
.expireAfterAccess(10, TimeUnit.MINUTES)
|
||||
.build(new CacheLoader<Contexts, PermissionCache>() {
|
||||
@@ -103,7 +96,7 @@ public class UserCache implements UserData {
|
||||
|
||||
@Override
|
||||
public PermissionCache calculatePermissions(@NonNull Contexts contexts) {
|
||||
PermissionCache data = new PermissionCache(contexts, user, calculatorFactory);
|
||||
PermissionCache data = new PermissionCache(contexts, user, user.getPlugin().getCalculatorFactory());
|
||||
data.setPermissions(user.exportNodes(ExtractedContexts.generate(contexts), true));
|
||||
return data;
|
||||
}
|
||||
@@ -163,4 +156,9 @@ public class UserCache implements UserData {
|
||||
meta.cleanUp();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
permission.invalidateAll();
|
||||
meta.invalidateAll();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
package me.lucko.luckperms.common.commands.impl.misc;
|
||||
|
||||
import me.lucko.luckperms.api.Tristate;
|
||||
import me.lucko.luckperms.common.caching.UserCache;
|
||||
import me.lucko.luckperms.common.commands.Arg;
|
||||
import me.lucko.luckperms.common.commands.CommandException;
|
||||
import me.lucko.luckperms.common.commands.CommandResult;
|
||||
@@ -71,13 +70,7 @@ public class CheckCommand extends SingleCommand {
|
||||
return CommandResult.STATE_ERROR;
|
||||
}
|
||||
|
||||
UserCache data = user.getUserData();
|
||||
if (data == null) {
|
||||
Message.USER_NO_DATA.send(sender, user.getFriendlyName());
|
||||
return CommandResult.STATE_ERROR;
|
||||
}
|
||||
|
||||
Tristate tristate = data.getPermissionData(plugin.getContextForUser(user)).getPermissionValue(permission);
|
||||
Tristate tristate = user.getUserData().getPermissionData(plugin.getContextForUser(user)).getPermissionValue(permission);
|
||||
Message.CHECK_RESULT.send(sender, user.getFriendlyName(), permission, Util.formatTristate(tristate));
|
||||
return CommandResult.SUCCESS;
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
package me.lucko.luckperms.common.commands.impl.misc;
|
||||
|
||||
import me.lucko.luckperms.api.caching.PermissionData;
|
||||
import me.lucko.luckperms.common.caching.UserCache;
|
||||
import me.lucko.luckperms.common.commands.Arg;
|
||||
import me.lucko.luckperms.common.commands.CommandException;
|
||||
import me.lucko.luckperms.common.commands.CommandResult;
|
||||
@@ -90,13 +89,7 @@ public class TreeCommand extends SingleCommand {
|
||||
return CommandResult.STATE_ERROR;
|
||||
}
|
||||
|
||||
UserCache data = user.getUserData();
|
||||
if (data == null) {
|
||||
Message.USER_NO_DATA.send(sender, user.getFriendlyName());
|
||||
return CommandResult.STATE_ERROR;
|
||||
}
|
||||
|
||||
PermissionData permissionData = data.getPermissionData(plugin.getContextForUser(user));
|
||||
PermissionData permissionData = user.getUserData().getPermissionData(plugin.getContextForUser(user));
|
||||
TreeView view = TreeViewBuilder.newBuilder().rootPosition(selection).maxLevels(maxLevel).build(plugin.getPermissionVault());
|
||||
|
||||
if (!view.hasData()) {
|
||||
|
||||
@@ -28,7 +28,6 @@ package me.lucko.luckperms.common.commands.impl.user;
|
||||
import me.lucko.luckperms.api.Contexts;
|
||||
import me.lucko.luckperms.api.Node;
|
||||
import me.lucko.luckperms.api.caching.MetaData;
|
||||
import me.lucko.luckperms.api.caching.UserData;
|
||||
import me.lucko.luckperms.common.commands.CommandException;
|
||||
import me.lucko.luckperms.common.commands.CommandResult;
|
||||
import me.lucko.luckperms.common.commands.abstraction.SubCommand;
|
||||
@@ -90,28 +89,25 @@ public class UserInfo extends SubCommand<User> {
|
||||
}
|
||||
}
|
||||
|
||||
UserData data = user.getUserData();
|
||||
String context = "&bNone";
|
||||
String prefix = "&bNone";
|
||||
String suffix = "&bNone";
|
||||
if (data != null) {
|
||||
Contexts contexts = plugin.getContextForUser(user);
|
||||
if (contexts != null) {
|
||||
context = contexts.getContexts().toSet().stream()
|
||||
.map(e -> Util.contextToString(e.getKey(), e.getValue()))
|
||||
.collect(Collectors.joining(" "));
|
||||
Contexts contexts = plugin.getContextForUser(user);
|
||||
if (contexts != null) {
|
||||
context = contexts.getContexts().toSet().stream()
|
||||
.map(e -> Util.contextToString(e.getKey(), e.getValue()))
|
||||
.collect(Collectors.joining(" "));
|
||||
|
||||
MetaData meta = data.getMetaData(contexts);
|
||||
if (meta.getPrefix() != null) {
|
||||
prefix = "&f\"" + meta.getPrefix() + "&f\"";
|
||||
}
|
||||
if (meta.getSuffix() != null) {
|
||||
suffix = "&f\"" + meta.getSuffix() + "&f\"";
|
||||
}
|
||||
MetaData meta = user.getUserData().getMetaData(contexts);
|
||||
if (meta.getPrefix() != null) {
|
||||
prefix = "&f\"" + meta.getPrefix() + "&f\"";
|
||||
}
|
||||
if (meta.getSuffix() != null) {
|
||||
suffix = "&f\"" + meta.getSuffix() + "&f\"";
|
||||
}
|
||||
}
|
||||
|
||||
Message.USER_INFO_DATA.send(sender, Util.formatBoolean(data != null), context, prefix, suffix);
|
||||
Message.USER_INFO_DATA.send(sender, Util.formatBoolean(contexts != null), context, prefix, suffix);
|
||||
return CommandResult.SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,7 +294,7 @@ public enum Message {
|
||||
|
||||
USER_INFO_DATA(
|
||||
"{PREFIX}&f- &aCached Data:" + "\n" +
|
||||
"{PREFIX}&f- &3Is Loaded: {0}" + "\n" +
|
||||
"{PREFIX}&f- &3Has contextual data: {0}" + "\n" +
|
||||
"{PREFIX}&f- &3Current Contexts: {1}" + "\n" +
|
||||
"{PREFIX}&f- &3Current Prefix: {2}" + "\n" +
|
||||
"{PREFIX}&f- &3Current Suffix: {3}",
|
||||
|
||||
@@ -29,7 +29,6 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
|
||||
import me.lucko.luckperms.api.caching.UserData;
|
||||
import me.lucko.luckperms.common.api.delegates.UserDelegate;
|
||||
import me.lucko.luckperms.common.caching.UserCache;
|
||||
import me.lucko.luckperms.common.caching.handlers.HolderReference;
|
||||
@@ -69,7 +68,7 @@ public class User extends PermissionHolder implements Identifiable<UserIdentifie
|
||||
* The users data cache instance, if present.
|
||||
*/
|
||||
@Getter
|
||||
private UserCache userData = null;
|
||||
private final UserCache userData;
|
||||
|
||||
@Getter
|
||||
private BufferedRequest<Void> refreshBuffer = new BufferedRequest<Void>(1000L, r -> getPlugin().doAsync(r)) {
|
||||
@@ -86,15 +85,20 @@ public class User extends PermissionHolder implements Identifiable<UserIdentifie
|
||||
public User(UUID uuid, LuckPermsPlugin plugin) {
|
||||
super(uuid.toString(), plugin);
|
||||
this.uuid = uuid;
|
||||
|
||||
this.primaryGroup = plugin.getConfiguration().get(ConfigKeys.PRIMARY_GROUP_CALCULATION).apply(this);
|
||||
this.userData = new UserCache(this);
|
||||
getPlugin().getApiProvider().getEventFactory().handleUserCacheLoad(this, userData);
|
||||
}
|
||||
|
||||
public User(UUID uuid, String name, LuckPermsPlugin plugin) {
|
||||
super(uuid.toString(), plugin);
|
||||
this.uuid = uuid;
|
||||
setName(name, true);
|
||||
|
||||
setName(name, true);
|
||||
this.primaryGroup = plugin.getConfiguration().get(ConfigKeys.PRIMARY_GROUP_CALCULATION).apply(this);
|
||||
this.userData = new UserCache(this);
|
||||
getPlugin().getApiProvider().getEventFactory().handleUserCacheLoad(this, userData);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -156,15 +160,8 @@ public class User extends PermissionHolder implements Identifiable<UserIdentifie
|
||||
* Sets up the UserData cache
|
||||
* Blocking call.
|
||||
*/
|
||||
public synchronized void setupData(boolean op) {
|
||||
if (userData != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
userData = new UserCache(this, getPlugin().getCalculatorFactory());
|
||||
public synchronized void preCalculateData(boolean op) {
|
||||
userData.preCalculate(getPlugin().getPreProcessContexts(op));
|
||||
|
||||
getPlugin().getApiProvider().getEventFactory().handleUserCacheLoad(this, userData);
|
||||
getPlugin().onUserRefresh(this);
|
||||
}
|
||||
|
||||
@@ -172,7 +169,7 @@ public class User extends PermissionHolder implements Identifiable<UserIdentifie
|
||||
* Removes the UserData cache from this user
|
||||
*/
|
||||
public void unregisterData() {
|
||||
userData = null;
|
||||
userData.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -180,14 +177,9 @@ public class User extends PermissionHolder implements Identifiable<UserIdentifie
|
||||
* Blocking call.
|
||||
*/
|
||||
private synchronized void refreshPermissions() {
|
||||
UserData ud = userData;
|
||||
if (ud == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
ud.recalculatePermissions();
|
||||
ud.recalculateMeta();
|
||||
getPlugin().getApiProvider().getEventFactory().handleUserDataRecalculate(this, ud);
|
||||
userData.recalculatePermissions();
|
||||
userData.recalculateMeta();
|
||||
getPlugin().getApiProvider().getEventFactory().handleUserDataRecalculate(this, userData);
|
||||
getPlugin().onUserRefresh(this);
|
||||
}
|
||||
|
||||
@@ -213,9 +205,6 @@ public class User extends PermissionHolder implements Identifiable<UserIdentifie
|
||||
}
|
||||
|
||||
public void cleanup() {
|
||||
UserCache cache = userData;
|
||||
if (cache != null) {
|
||||
cache.cleanup();
|
||||
}
|
||||
userData.cleanup();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ public class LoginHelper {
|
||||
plugin.getStorage().force().saveUser(user).join();
|
||||
}
|
||||
|
||||
user.setupData(false); // Pretty nasty calculation call. Sets up the caching system so data is ready when the user joins.
|
||||
user.preCalculateData(false); // Pretty nasty calculation call. Sets up the caching system so data is ready when the user joins.
|
||||
}
|
||||
|
||||
final long time = System.currentTimeMillis() - startTime;
|
||||
|
||||
Reference in New Issue
Block a user