Populate Subject cache when obtaining SubjectReferences for instances which already exist

This commit is contained in:
Luck 2017-12-30 15:23:31 +00:00
parent b9dfd7db54
commit 764665cf15
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
17 changed files with 95 additions and 42 deletions

View File

@ -32,6 +32,7 @@ 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;
import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection;
import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory;
import org.spongepowered.api.service.context.Context; import org.spongepowered.api.service.context.Context;
import org.spongepowered.api.service.permission.Subject; import org.spongepowered.api.service.permission.Subject;
@ -71,7 +72,7 @@ public final class SubjectCollectionProxy implements SubjectCollection {
// this behaviour should be replaced when CompletableFutures are added to Sponge // this behaviour should be replaced when CompletableFutures are added to Sponge
return (List) handle.getAllIdentifiers() return (List) handle.getAllIdentifiers()
.thenApply(ids -> ids.stream() .thenApply(ids -> ids.stream()
.map(s -> new SubjectProxy(service, service.newSubjectReference(getIdentifier(), s))) .map(s -> new SubjectProxy(service, SubjectReferenceFactory.obtain(service, getIdentifier(), s)))
.collect(ImmutableCollectors.toList()) .collect(ImmutableCollectors.toList())
).join(); ).join();
} }

View File

@ -33,6 +33,7 @@ import me.lucko.luckperms.sponge.service.model.LPPermissionService;
import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubject;
import me.lucko.luckperms.sponge.service.model.LPSubjectData; import me.lucko.luckperms.sponge.service.model.LPSubjectData;
import me.lucko.luckperms.sponge.service.model.SubjectReference; import me.lucko.luckperms.sponge.service.model.SubjectReference;
import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory;
import org.spongepowered.api.service.context.Context; import org.spongepowered.api.service.context.Context;
import org.spongepowered.api.service.permission.Subject; import org.spongepowered.api.service.permission.Subject;
@ -114,10 +115,7 @@ public final class SubjectDataProxy implements SubjectData {
public boolean addParent(Set<Context> contexts, Subject parent) { public boolean addParent(Set<Context> contexts, Subject parent) {
handle().thenCompose(handle -> handle.addParent( handle().thenCompose(handle -> handle.addParent(
CompatibilityUtil.convertContexts(contexts), CompatibilityUtil.convertContexts(contexts),
service.newSubjectReference( SubjectReferenceFactory.obtain(service, parent)
parent.getContainingCollection().getIdentifier(),
parent.getIdentifier()
)
)); ));
return true; return true;
} }
@ -126,10 +124,7 @@ public final class SubjectDataProxy implements SubjectData {
public boolean removeParent(Set<Context> contexts, Subject parent) { public boolean removeParent(Set<Context> contexts, Subject parent) {
handle().thenCompose(handle -> handle.removeParent( handle().thenCompose(handle -> handle.removeParent(
CompatibilityUtil.convertContexts(contexts), CompatibilityUtil.convertContexts(contexts),
service.newSubjectReference( SubjectReferenceFactory.obtain(service, parent)
parent.getContainingCollection().getIdentifier(),
parent.getIdentifier()
)
)); ));
return true; return true;
} }

View File

@ -32,7 +32,9 @@ 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;
import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubject;
import me.lucko.luckperms.sponge.service.model.ProxiedSubject;
import me.lucko.luckperms.sponge.service.model.SubjectReference; import me.lucko.luckperms.sponge.service.model.SubjectReference;
import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory;
import org.spongepowered.api.command.CommandSource; import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.service.context.Context; import org.spongepowered.api.service.context.Context;
@ -48,7 +50,7 @@ import java.util.concurrent.CompletableFuture;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@RequiredArgsConstructor @RequiredArgsConstructor
public final class SubjectProxy implements Subject { public final class SubjectProxy implements Subject, ProxiedSubject {
private final LPPermissionService service; private final LPPermissionService service;
private final SubjectReference ref; private final SubjectReference ref;
@ -56,6 +58,11 @@ public final class SubjectProxy implements Subject {
return ref.resolveLp(); return ref.resolveLp();
} }
@Override
public SubjectReference getReference() {
return ref;
}
@Override @Override
public Optional<CommandSource> getCommandSource() { public Optional<CommandSource> getCommandSource() {
return handle().thenApply(LPSubject::getCommandSource).join(); return handle().thenApply(LPSubject::getCommandSource).join();
@ -95,10 +102,7 @@ public final class SubjectProxy implements Subject {
public boolean isChildOf(Subject parent) { public boolean isChildOf(Subject parent) {
return handle().thenApply(handle -> handle.isChildOf( return handle().thenApply(handle -> handle.isChildOf(
ImmutableContextSet.empty(), ImmutableContextSet.empty(),
service.newSubjectReference( SubjectReferenceFactory.obtain(service, parent)
parent.getContainingCollection().getIdentifier(),
parent.getIdentifier()
)
)).join(); )).join();
} }
@ -106,10 +110,7 @@ public final class SubjectProxy implements Subject {
public boolean isChildOf(Set<Context> contexts, Subject parent) { public boolean isChildOf(Set<Context> contexts, Subject parent) {
return handle().thenApply(handle -> handle.isChildOf( return handle().thenApply(handle -> handle.isChildOf(
CompatibilityUtil.convertContexts(contexts), CompatibilityUtil.convertContexts(contexts),
service.newSubjectReference( SubjectReferenceFactory.obtain(service, parent)
parent.getContainingCollection().getIdentifier(),
parent.getIdentifier()
)
)).join(); )).join();
} }

View File

@ -33,6 +33,7 @@ import me.lucko.luckperms.common.utils.ImmutableCollectors;
import me.lucko.luckperms.sponge.service.model.LPPermissionDescription; import me.lucko.luckperms.sponge.service.model.LPPermissionDescription;
import me.lucko.luckperms.sponge.service.model.LPPermissionService; import me.lucko.luckperms.sponge.service.model.LPPermissionService;
import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection;
import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory;
import org.spongepowered.api.Sponge; import org.spongepowered.api.Sponge;
import org.spongepowered.api.plugin.PluginContainer; import org.spongepowered.api.plugin.PluginContainer;
@ -105,7 +106,7 @@ public final class PermissionServiceProxy implements PermissionService {
@Override @Override
public SubjectReference newSubjectReference(String s, String s1) { public SubjectReference newSubjectReference(String s, String s1) {
return handle.newSubjectReference(s, s1); return SubjectReferenceFactory.obtain(handle, s, s1);
} }
@Override @Override

View File

@ -31,6 +31,7 @@ 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.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubject;
import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection;
import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory;
import org.spongepowered.api.service.context.Context; import org.spongepowered.api.service.context.Context;
import org.spongepowered.api.service.permission.Subject; import org.spongepowered.api.service.permission.Subject;
@ -91,7 +92,7 @@ public final class SubjectCollectionProxy implements SubjectCollection {
@Override @Override
public SubjectReference newSubjectReference(String s) { public SubjectReference newSubjectReference(String s) {
return handle.newSubjectReference(s); return SubjectReferenceFactory.obtain(handle.getService(), getIdentifier(), s);
} }
@Override @Override

View File

@ -31,6 +31,7 @@ import me.lucko.luckperms.api.context.ImmutableContextSet;
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;
import me.lucko.luckperms.sponge.service.model.ProxiedSubject;
import me.lucko.luckperms.sponge.service.model.SubjectReference; import me.lucko.luckperms.sponge.service.model.SubjectReference;
import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory; import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory;
@ -48,7 +49,7 @@ import java.util.concurrent.CompletableFuture;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@RequiredArgsConstructor @RequiredArgsConstructor
public final class SubjectProxy implements Subject { public final class SubjectProxy implements Subject, ProxiedSubject {
private final LPPermissionService service; private final LPPermissionService service;
private final SubjectReference ref; private final SubjectReference ref;
@ -56,6 +57,11 @@ public final class SubjectProxy implements Subject {
return ref.resolveLp(); return ref.resolveLp();
} }
@Override
public SubjectReference getReference() {
return ref;
}
@Override @Override
public Optional<CommandSource> getCommandSource() { public Optional<CommandSource> getCommandSource() {
return getHandle().thenApply(LPSubject::getCommandSource).join(); return getHandle().thenApply(LPSubject::getCommandSource).join();

View File

@ -64,8 +64,6 @@ public interface LPPermissionService {
ImmutableMap<String, LPSubjectCollection> getLoadedCollections(); ImmutableMap<String, LPSubjectCollection> getLoadedCollections();
SubjectReference newSubjectReference(String collectionIdentifier, String subjectIdentifier);
LPPermissionDescription registerPermissionDescription(String id, Text description, PluginContainer owner); LPPermissionDescription registerPermissionDescription(String id, Text description, PluginContainer owner);
Optional<LPPermissionDescription> getDescription(String permission); Optional<LPPermissionDescription> getDescription(String permission);

View File

@ -47,7 +47,7 @@ public interface LPSubject {
String getIdentifier(); String getIdentifier();
default SubjectReference toReference() { default SubjectReference toReference() {
return getService().newSubjectReference(getParentCollection().getIdentifier(), getIdentifier()); return SubjectReferenceFactory.obtain(getService(), this);
} }
default LPSubject getDefaults() { default LPSubject getDefaults() {

View File

@ -49,10 +49,6 @@ public interface LPSubjectCollection {
String getIdentifier(); String getIdentifier();
default SubjectReference newSubjectReference(String identifier) {
return getService().newSubjectReference(getIdentifier(), identifier);
}
Predicate<String> getIdentifierValidityPredicate(); Predicate<String> getIdentifierValidityPredicate();
CompletableFuture<LPSubject> loadSubject(String identifier); CompletableFuture<LPSubject> loadSubject(String identifier);

View File

@ -0,0 +1,35 @@
/*
* 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.sponge.service.model;
/**
* Marks that an object is a proxied representation of a {@link org.spongepowered.api.service.permission.Subject}.
*/
public interface ProxiedSubject {
SubjectReference getReference();
}

View File

@ -48,7 +48,7 @@ public final class SubjectReference implements org.spongepowered.api.service.per
/** /**
* The time a subject instance should be cached in this reference * The time a subject instance should be cached in this reference
*/ */
private static final long CACHE_TIME = TimeUnit.SECONDS.toMillis(60); private static final long CACHE_TIME = TimeUnit.MINUTES.toMillis(5);
/** /**
* Reference to the permission service * Reference to the permission service
@ -79,6 +79,19 @@ public final class SubjectReference implements org.spongepowered.api.service.per
this.subjectIdentifier = Preconditions.checkNotNull(subjectIdentifier); this.subjectIdentifier = Preconditions.checkNotNull(subjectIdentifier);
} }
void fillCache(LPSubject subject) {
LPSubject sub = tryCache();
if (sub == null) {
// if no value is currently cached, populate with the passed value
lastLookup = System.currentTimeMillis();
cache = new WeakReference<>(subject);
} else if (sub == subject) {
// if equal, reset the cache timeout
lastLookup = System.currentTimeMillis();
}
}
private LPSubject tryCache() { private LPSubject tryCache() {
if ((System.currentTimeMillis() - lastLookup) < CACHE_TIME) { if ((System.currentTimeMillis() - lastLookup) < CACHE_TIME) {
if (cache != null) { if (cache != null) {
@ -152,7 +165,7 @@ public final class SubjectReference implements org.spongepowered.api.service.per
@Override @Override
public String toString() { public String toString() {
return "SubjectReference(" + return "luckperms.SubjectReference(" +
"collection=" + this.collectionIdentifier + ", " + "collection=" + this.collectionIdentifier + ", " +
"subject=" + this.subjectIdentifier + ")"; "subject=" + this.subjectIdentifier + ")";
} }

View File

@ -63,7 +63,17 @@ public final class SubjectReferenceFactory {
return obtain(service, parts.get(0), parts.get(1)); return obtain(service, parts.get(0), parts.get(1));
} }
public static SubjectReference obtain(@NonNull LPPermissionService service, @NonNull LPSubject subject) {
SubjectReference ret = obtain(service, subject.getParentCollection().getIdentifier(), subject.getIdentifier());
ret.fillCache(subject);
return ret;
}
public static SubjectReference obtain(@NonNull LPPermissionService service, @NonNull Subject subject) { public static SubjectReference obtain(@NonNull LPPermissionService service, @NonNull Subject subject) {
if (subject instanceof ProxiedSubject) {
return ((ProxiedSubject) subject).getReference();
}
return obtain(service, subject.getContainingCollection().getIdentifier(), subject.getIdentifier()); return obtain(service, subject.getContainingCollection().getIdentifier(), subject.getIdentifier());
} }

View File

@ -50,6 +50,7 @@ import me.lucko.luckperms.sponge.service.ProxyFactory;
import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubject;
import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection;
import me.lucko.luckperms.sponge.service.model.SubjectReference; import me.lucko.luckperms.sponge.service.model.SubjectReference;
import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory;
import org.spongepowered.api.service.permission.PermissionService; import org.spongepowered.api.service.permission.PermissionService;
import org.spongepowered.api.service.permission.SubjectCollection; import org.spongepowered.api.service.permission.SubjectCollection;
@ -237,7 +238,7 @@ public class SpongeGroupManager implements GroupManager, LPSubjectCollection {
List<HeldPermission<String>> lookup = plugin.getStorage().getGroupsWithPermission(permission).join(); List<HeldPermission<String>> lookup = plugin.getStorage().getGroupsWithPermission(permission).join();
for (HeldPermission<String> holder : lookup) { for (HeldPermission<String> holder : lookup) {
if (holder.asNode().getFullContexts().equals(ImmutableContextSet.empty())) { if (holder.asNode().getFullContexts().equals(ImmutableContextSet.empty())) {
ret.put(getService().newSubjectReference(getIdentifier(), holder.getHolder()), holder.getValue()); ret.put(SubjectReferenceFactory.obtain(getService(), getIdentifier(), holder.getHolder()), holder.getValue());
} }
} }
@ -253,7 +254,7 @@ public class SpongeGroupManager implements GroupManager, LPSubjectCollection {
List<HeldPermission<String>> lookup = plugin.getStorage().getGroupsWithPermission(permission).join(); List<HeldPermission<String>> lookup = plugin.getStorage().getGroupsWithPermission(permission).join();
for (HeldPermission<String> holder : lookup) { for (HeldPermission<String> holder : lookup) {
if (holder.asNode().getFullContexts().equals(contexts)) { if (holder.asNode().getFullContexts().equals(contexts)) {
ret.put(getService().newSubjectReference(getIdentifier(), holder.getHolder()), holder.getValue()); ret.put(SubjectReferenceFactory.obtain(getService(), getIdentifier(), holder.getHolder()), holder.getValue());
} }
} }

View File

@ -50,6 +50,7 @@ import me.lucko.luckperms.sponge.service.ProxyFactory;
import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubject;
import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection;
import me.lucko.luckperms.sponge.service.model.SubjectReference; import me.lucko.luckperms.sponge.service.model.SubjectReference;
import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory;
import org.spongepowered.api.service.permission.PermissionService; import org.spongepowered.api.service.permission.PermissionService;
import org.spongepowered.api.service.permission.SubjectCollection; import org.spongepowered.api.service.permission.SubjectCollection;
@ -334,7 +335,7 @@ public class SpongeUserManager implements UserManager, LPSubjectCollection {
List<HeldPermission<UUID>> lookup = plugin.getStorage().getUsersWithPermission(permission).join(); List<HeldPermission<UUID>> lookup = plugin.getStorage().getUsersWithPermission(permission).join();
for (HeldPermission<UUID> holder : lookup) { for (HeldPermission<UUID> holder : lookup) {
if (holder.asNode().getFullContexts().equals(ImmutableContextSet.empty())) { if (holder.asNode().getFullContexts().equals(ImmutableContextSet.empty())) {
ret.put(getService().newSubjectReference(getIdentifier(), holder.getHolder().toString()), holder.getValue()); ret.put(SubjectReferenceFactory.obtain(getService(), getIdentifier(), holder.getHolder().toString()), holder.getValue());
} }
} }
@ -350,7 +351,7 @@ public class SpongeUserManager implements UserManager, LPSubjectCollection {
List<HeldPermission<UUID>> lookup = plugin.getStorage().getUsersWithPermission(permission).join(); List<HeldPermission<UUID>> lookup = plugin.getStorage().getUsersWithPermission(permission).join();
for (HeldPermission<UUID> holder : lookup) { for (HeldPermission<UUID> holder : lookup) {
if (holder.asNode().getFullContexts().equals(contexts)) { if (holder.asNode().getFullContexts().equals(contexts)) {
ret.put(getService().newSubjectReference(getIdentifier(), holder.getHolder().toString()), holder.getValue()); ret.put(SubjectReferenceFactory.obtain(getService(), getIdentifier(), holder.getHolder().toString()), holder.getValue());
} }
} }

View File

@ -47,7 +47,6 @@ import me.lucko.luckperms.sponge.service.model.LPPermissionService;
import me.lucko.luckperms.sponge.service.model.LPSubject; import me.lucko.luckperms.sponge.service.model.LPSubject;
import me.lucko.luckperms.sponge.service.model.LPSubjectCollection; import me.lucko.luckperms.sponge.service.model.LPSubjectCollection;
import me.lucko.luckperms.sponge.service.model.SubjectReference; import me.lucko.luckperms.sponge.service.model.SubjectReference;
import me.lucko.luckperms.sponge.service.model.SubjectReferenceFactory;
import me.lucko.luckperms.sponge.service.persisted.PersistedCollection; import me.lucko.luckperms.sponge.service.persisted.PersistedCollection;
import me.lucko.luckperms.sponge.service.storage.SubjectStorage; import me.lucko.luckperms.sponge.service.storage.SubjectStorage;
@ -142,11 +141,6 @@ public class LuckPermsService implements LPPermissionService {
return ImmutableMap.copyOf(collections.asMap()); return ImmutableMap.copyOf(collections.asMap());
} }
@Override
public SubjectReference newSubjectReference(String collectionIdentifier, String subjectIdentifier) {
return SubjectReferenceFactory.obtain(this, collectionIdentifier, subjectIdentifier);
}
@Override @Override
public LPPermissionDescription registerPermissionDescription(String id, Text description, PluginContainer owner) { public LPPermissionDescription registerPermissionDescription(String id, Text description, PluginContainer owner) {
LuckPermsPermissionDescription desc = new LuckPermsPermissionDescription(this, id, description, owner); LuckPermsPermissionDescription desc = new LuckPermsPermissionDescription(this, id, description, owner);