Invalidate all user/group caches after node changes are made to groups via the API

This commit is contained in:
Luck 2019-12-29 01:17:30 +00:00
parent d3b3a8af38
commit b2593d409e
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
3 changed files with 43 additions and 19 deletions

View File

@ -82,6 +82,14 @@ public class ApiGroup extends ApiPermissionHolder implements net.luckperms.api.m
return this.handle.getCachedData();
}
@Override
protected void onNodeChange() {
// invalidate caches - they have potentially been affected by
// this change.
this.handle.getPlugin().getGroupManager().invalidateAllGroupCaches();
this.handle.getPlugin().getUserManager().invalidateAllUserCaches();
}
@Override
public boolean equals(Object o) {
if (o == this) return true;

View File

@ -75,12 +75,7 @@ public class ApiGroupManager extends ApiAbstractManager<Group, net.luckperms.api
@Override
public @NonNull CompletableFuture<Void> saveGroup(net.luckperms.api.model.group.@NonNull Group group) {
Objects.requireNonNull(group, "group");
return this.plugin.getStorage().saveGroup(ApiGroup.cast(group)).thenRun(() -> {
// invalidate caches - they have potentially been affected by
// this change.
this.plugin.getGroupManager().invalidateAllGroupCaches();
this.plugin.getUserManager().invalidateAllUserCaches();
});
return this.plugin.getStorage().saveGroup(ApiGroup.cast(group));
}
@Override
@ -90,12 +85,7 @@ public class ApiGroupManager extends ApiAbstractManager<Group, net.luckperms.api
throw new IllegalArgumentException("Cannot delete the default group.");
}
return this.plugin.getStorage().deleteGroup(ApiGroup.cast(group), DeletionCause.API).thenRun(() -> {
// invalidate caches - they have potentially been affected by
// this change.
this.plugin.getGroupManager().invalidateAllGroupCaches();
this.plugin.getUserManager().invalidateAllUserCaches();
});
return this.plugin.getStorage().deleteGroup(ApiGroup.cast(group), DeletionCause.API);
}
@Override

View File

@ -65,6 +65,12 @@ public class ApiPermissionHolder implements net.luckperms.api.model.PermissionHo
return this.handle;
}
protected void onNodeChange() {
// overridden by groups
// when a node is changed on a group, it could potentially affect other groups/users,
// so their caches need to be invalidated too. we handle this automatically for API users.
}
@Override
public @NonNull Identifier getIdentifier() {
return this.handle.getIdentifier();
@ -151,38 +157,58 @@ public class ApiPermissionHolder implements net.luckperms.api.model.PermissionHo
@Override
public @NonNull DataMutateResult add(@NonNull Node node) {
return ApiPermissionHolder.this.handle.setNode(this.dataType, node, true);
DataMutateResult result = ApiPermissionHolder.this.handle.setNode(this.dataType, node, true);
if (result.wasSuccessful()) {
onNodeChange();
}
return result;
}
@Override
public DataMutateResult.@NonNull WithMergedNode add(@NonNull Node node, @NonNull TemporaryNodeMergeStrategy temporaryNodeMergeStrategy) {
return ApiPermissionHolder.this.handle.setNode(this.dataType, node, temporaryNodeMergeStrategy);
DataMutateResult.WithMergedNode result = ApiPermissionHolder.this.handle.setNode(this.dataType, node, temporaryNodeMergeStrategy);
if (result.getResult().wasSuccessful()) {
onNodeChange();
}
return result;
}
@Override
public @NonNull DataMutateResult remove(@NonNull Node node) {
return ApiPermissionHolder.this.handle.unsetNode(this.dataType, node);
DataMutateResult result = ApiPermissionHolder.this.handle.unsetNode(this.dataType, node);
if (result.wasSuccessful()) {
onNodeChange();
}
return result;
}
@Override
public void clear() {
ApiPermissionHolder.this.handle.clearNodes(this.dataType, null, true);
if (ApiPermissionHolder.this.handle.clearNodes(this.dataType, null, true)) {
onNodeChange();
}
}
@Override
public void clear(@NonNull Predicate<? super Node> test) {
ApiPermissionHolder.this.handle.removeIf(this.dataType, null, test, true);
if (ApiPermissionHolder.this.handle.removeIf(this.dataType, null, test, true)) {
onNodeChange();
}
}
@Override
public void clear(@NonNull ContextSet contextSet) {
ApiPermissionHolder.this.handle.clearNodes(this.dataType, contextSet, true);
if (ApiPermissionHolder.this.handle.clearNodes(this.dataType, contextSet, true)) {
onNodeChange();
}
}
@Override
public void clear(@NonNull ContextSet contextSet, @NonNull Predicate<? super Node> test) {
ApiPermissionHolder.this.handle.removeIf(this.dataType, contextSet, test, true);
if (ApiPermissionHolder.this.handle.removeIf(this.dataType, contextSet, test, true)) {
onNodeChange();
}
}
}