replace LoadingCache with more simple LoadingMap alternative when no auto expiry is needed

This commit is contained in:
Luck 2018-12-14 18:37:56 +00:00
parent de24817d9c
commit 4b1cf51530
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
17 changed files with 83 additions and 124 deletions

View File

@ -40,8 +40,6 @@ import me.lucko.luckperms.api.platform.PlatformInfo;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import javafx.print.Collation;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;

View File

@ -36,6 +36,7 @@ import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.context.ContextManager;
import me.lucko.luckperms.common.context.ContextsCache;
import me.lucko.luckperms.common.context.ContextsSupplier;
import me.lucko.luckperms.common.util.LoadingMap;
import org.bukkit.entity.Player;
@ -45,8 +46,7 @@ import java.util.concurrent.TimeUnit;
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));
private final LoadingMap<Player, ContextsCache<Player>> onlineSubjectCaches = LoadingMap.of(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()
@ -64,7 +64,7 @@ public class BukkitContextManager extends ContextManager<Player> {
}
public void onPlayerQuit(Player player) {
this.onlineSubjectCaches.invalidate(player);
this.onlineSubjectCaches.remove(player);
}
@Override

View File

@ -25,14 +25,12 @@
package me.lucko.luckperms.bukkit.inject.server;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.collect.ForwardingMap;
import com.google.common.collect.ImmutableMap;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.treeview.PermissionRegistry;
import me.lucko.luckperms.common.util.LoadingMap;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.PluginManager;
@ -44,6 +42,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
/**
* A replacement map for the 'permissions' instance in Bukkit's SimplePluginManager.
@ -62,11 +61,8 @@ public final class LPPermissionMap extends ForwardingMap<String, Permission> {
private final Map<String, Permission> delegate = new ConcurrentHashMap<>();
// cache from permission --> children
private final LoadingCache<String, Map<String, Boolean>> trueChildPermissions = Caffeine.newBuilder()
.build(new ChildPermissionResolver(true));
private final LoadingCache<String, Map<String, Boolean>> falseChildPermissions = Caffeine.newBuilder()
.build(new ChildPermissionResolver(false));
private final Map<String, Map<String, Boolean>> trueChildPermissions = LoadingMap.of(new ChildPermissionResolver(true));
private final Map<String, Map<String, Boolean>> falseChildPermissions = LoadingMap.of(new ChildPermissionResolver(false));
/**
* The plugin instance
@ -83,8 +79,8 @@ public final class LPPermissionMap extends ForwardingMap<String, Permission> {
}
private void update() {
this.trueChildPermissions.invalidateAll();
this.falseChildPermissions.invalidateAll();
this.trueChildPermissions.clear();
this.falseChildPermissions.clear();
}
@Override
@ -154,7 +150,7 @@ public final class LPPermissionMap extends ForwardingMap<String, Permission> {
return super.get(key);
}
private final class ChildPermissionResolver implements CacheLoader<String, Map<String, Boolean>> {
private final class ChildPermissionResolver implements Function<String, Map<String, Boolean>> {
private final boolean value;
private ChildPermissionResolver(boolean value) {
@ -162,7 +158,7 @@ public final class LPPermissionMap extends ForwardingMap<String, Permission> {
}
@Override
public Map<String, Boolean> load(@NonNull String key) {
public Map<String, Boolean> apply(@NonNull String key) {
Map<String, Boolean> children = new HashMap<>();
resolveChildren(children, Collections.singletonMap(key, this.value), false);
children.remove(key, this.value);

View File

@ -25,8 +25,6 @@
package me.lucko.luckperms.common.commands.group;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.collect.Maps;
import me.lucko.luckperms.api.HeldPermission;
@ -52,6 +50,7 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.sender.Sender;
import me.lucko.luckperms.common.util.DurationFormatter;
import me.lucko.luckperms.common.util.Iterators;
import me.lucko.luckperms.common.util.LoadingMap;
import me.lucko.luckperms.common.util.Predicates;
import me.lucko.luckperms.common.util.TextUtils;
@ -99,22 +98,21 @@ public class GroupListMembers extends SubCommand<Group> {
Message.SEARCH_RESULT.send(sender, users + groups, users, groups);
if (!matchedUsers.isEmpty()) {
LoadingCache<UUID, String> uuidLookups = Caffeine.newBuilder()
.build(u -> {
String s = plugin.getStorage().getPlayerName(u).join();
if (s != null && !s.isEmpty() && !s.equals("null")) {
return s;
}
Map<UUID, String> uuidLookups = LoadingMap.of(u -> {
String s = plugin.getStorage().getPlayerName(u).join();
if (s != null && !s.isEmpty() && !s.equals("null")) {
return s;
}
if (plugin.getConfiguration().get(ConfigKeys.USE_SERVER_UUID_CACHE)) {
s = plugin.getBootstrap().lookupUsername(u).orElse(null);
if (s != null) {
return s;
}
}
if (plugin.getConfiguration().get(ConfigKeys.USE_SERVER_UUID_CACHE)) {
s = plugin.getBootstrap().lookupUsername(u).orElse(null);
if (s != null) {
return s;
}
}
return u.toString();
});
return u.toString();
});
sendResult(sender, matchedUsers, uuidLookups::get, Message.SEARCH_SHOWING_USERS, HolderType.USER, label, page);
}

View File

@ -25,8 +25,6 @@
package me.lucko.luckperms.common.commands.misc;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.collect.Maps;
import me.lucko.luckperms.api.HeldPermission;
@ -53,6 +51,7 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.sender.Sender;
import me.lucko.luckperms.common.util.DurationFormatter;
import me.lucko.luckperms.common.util.Iterators;
import me.lucko.luckperms.common.util.LoadingMap;
import me.lucko.luckperms.common.util.Predicates;
import me.lucko.luckperms.common.util.TextUtils;
@ -96,22 +95,21 @@ public class SearchCommand extends SingleCommand {
Message.SEARCH_RESULT.send(sender, users + groups, users, groups);
if (!matchedUsers.isEmpty()) {
LoadingCache<UUID, String> uuidLookups = Caffeine.newBuilder()
.build(u -> {
String s = plugin.getStorage().getPlayerName(u).join();
if (s != null && !s.isEmpty() && !s.equals("null")) {
return s;
}
Map<UUID, String> uuidLookups = LoadingMap.of(u -> {
String s = plugin.getStorage().getPlayerName(u).join();
if (s != null && !s.isEmpty() && !s.equals("null")) {
return s;
}
if (plugin.getConfiguration().get(ConfigKeys.USE_SERVER_UUID_CACHE)) {
s = plugin.getBootstrap().lookupUsername(u).orElse(null);
if (s != null) {
return s;
}
}
if (plugin.getConfiguration().get(ConfigKeys.USE_SERVER_UUID_CACHE)) {
s = plugin.getBootstrap().lookupUsername(u).orElse(null);
if (s != null) {
return s;
}
}
return u.toString();
});
return u.toString();
});
sendResult(sender, matchedUsers, uuidLookups::get, Message.SEARCH_SHOWING_USERS, HolderType.USER, label, page, comparison);
}

View File

@ -25,14 +25,13 @@
package me.lucko.luckperms.common.event.gen;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import me.lucko.luckperms.api.LuckPermsApi;
import me.lucko.luckperms.api.event.LuckPermsEvent;
import me.lucko.luckperms.api.event.Param;
import me.lucko.luckperms.common.util.ImmutableCollectors;
import me.lucko.luckperms.common.util.LoadingMap;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.InvocationHandler;
@ -42,6 +41,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
/**
* Represents the generated specification for an instance of a given {@link LuckPermsEvent}.
@ -63,8 +63,7 @@ public class GeneratedEventSpec {
}
}
private static final LoadingCache<Class<? extends LuckPermsEvent>, GeneratedEventSpec> CACHE = Caffeine.newBuilder()
.build(GeneratedEventSpec::new);
private static final Map<Class<? extends LuckPermsEvent>, GeneratedEventSpec> CACHE = LoadingMap.of(GeneratedEventSpec::new);
public static GeneratedEventSpec lookup(Class<? extends LuckPermsEvent> event) {
return CACHE.get(event);

View File

@ -25,14 +25,10 @@
package me.lucko.luckperms.common.model.manager;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
import me.lucko.luckperms.common.model.Identifiable;
import org.checkerframework.checker.nullness.qual.NonNull;
import me.lucko.luckperms.common.util.LoadingMap;
import java.util.Map;
@ -45,22 +41,11 @@ import java.util.Map;
*/
public abstract class AbstractManager<I, C extends Identifiable<I>, T extends C> implements Manager<I, C, T> {
private final LoadingCache<I, T> objects = Caffeine.newBuilder()
.build(new CacheLoader<I, T>() {
@Override
public T load(@NonNull I i) {
return apply(i);
}
@Override
public T reload(@NonNull I i, @NonNull T t) {
return t; // Never needs to be refreshed.
}
});
private final LoadingMap<I, T> objects = LoadingMap.of(this);
@Override
public Map<I, T> getAll() {
return ImmutableMap.copyOf(this.objects.asMap());
return ImmutableMap.copyOf(this.objects);
}
@Override
@ -75,13 +60,13 @@ public abstract class AbstractManager<I, C extends Identifiable<I>, T extends C>
@Override
public boolean isLoaded(I id) {
return this.objects.asMap().containsKey(sanitizeIdentifier(id));
return this.objects.containsKey(sanitizeIdentifier(id));
}
@Override
public void unload(I id) {
if (id != null) {
this.objects.invalidate(sanitizeIdentifier(id));
this.objects.remove(sanitizeIdentifier(id));
}
}
@ -94,7 +79,7 @@ public abstract class AbstractManager<I, C extends Identifiable<I>, T extends C>
@Override
public void unloadAll() {
this.objects.invalidateAll();
this.objects.clear();
}
protected I sanitizeIdentifier(I i) {

View File

@ -27,7 +27,6 @@ package me.lucko.luckperms.common.node.model;
import me.lucko.luckperms.api.Node;
import me.lucko.luckperms.api.NodeEqualityPredicate;
import me.lucko.luckperms.api.StandardNodeEquality;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.api.nodetype.NodeType;

View File

@ -36,7 +36,6 @@ import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* Stores a collection of all permissions known to the platform.

View File

@ -53,6 +53,10 @@ public class LoadingMap<K, V> extends ForwardingMap<K, V> implements Map<K, V> {
return this.map;
}
public V getIfPresent(K key) {
return this.map.get(key);
}
@Override
public V get(Object key) {
V value = this.map.get(key);

View File

@ -25,25 +25,22 @@
package me.lucko.luckperms.common.util;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public final class PatternCache {
private static final LoadingCache<String, CachedPattern> CACHE = Caffeine.newBuilder()
.build(s -> {
try {
return new CachedPattern(Pattern.compile(s));
} catch (PatternSyntaxException e) {
return new CachedPattern(e);
}
});
private static final Map<String, CachedPattern> CACHE = LoadingMap.of(s -> {
try {
return new CachedPattern(Pattern.compile(s));
} catch (PatternSyntaxException e) {
return new CachedPattern(e);
}
});
public static CachedPattern lookup(String regex) {
CachedPattern pattern = CACHE.get(regex);

View File

@ -35,6 +35,7 @@ import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.context.ContextManager;
import me.lucko.luckperms.common.context.ContextsCache;
import me.lucko.luckperms.common.context.ContextsSupplier;
import me.lucko.luckperms.common.util.LoadingMap;
import me.lucko.luckperms.nukkit.LPNukkitPlugin;
import cn.nukkit.Player;
@ -45,8 +46,7 @@ import java.util.concurrent.TimeUnit;
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));
private final LoadingMap<Player, ContextsCache<Player>> onlineSubjectCaches = LoadingMap.of(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()
@ -64,7 +64,7 @@ public class NukkitContextManager extends ContextManager<Player> {
}
public void onPlayerQuit(Player player) {
this.onlineSubjectCaches.invalidate(player);
this.onlineSubjectCaches.remove(player);
}
@Override

View File

@ -25,14 +25,12 @@
package me.lucko.luckperms.nukkit.inject.server;
import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.collect.ForwardingMap;
import com.google.common.collect.ImmutableMap;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.treeview.PermissionRegistry;
import me.lucko.luckperms.common.util.LoadingMap;
import org.checkerframework.checker.nullness.qual.NonNull;
@ -43,6 +41,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
/**
* A replacement map for the 'permissions' instance in Nukkit's SimplePluginManager.
@ -61,11 +60,8 @@ public final class LPPermissionMap extends ForwardingMap<String, Permission> {
private final Map<String, Permission> delegate = new ConcurrentHashMap<>();
// cache from permission --> children
private final LoadingCache<String, Map<String, Boolean>> trueChildPermissions = Caffeine.newBuilder()
.build(new ChildPermissionResolver(true));
private final LoadingCache<String, Map<String, Boolean>> falseChildPermissions = Caffeine.newBuilder()
.build(new ChildPermissionResolver(false));
private final Map<String, Map<String, Boolean>> trueChildPermissions = LoadingMap.of(new ChildPermissionResolver(true));
private final Map<String, Map<String, Boolean>> falseChildPermissions = LoadingMap.of(new ChildPermissionResolver(false));
/**
* The plugin instance
@ -82,8 +78,8 @@ public final class LPPermissionMap extends ForwardingMap<String, Permission> {
}
private void update() {
this.trueChildPermissions.invalidateAll();
this.falseChildPermissions.invalidateAll();
this.trueChildPermissions.clear();
this.falseChildPermissions.clear();
}
@Override
@ -114,7 +110,7 @@ public final class LPPermissionMap extends ForwardingMap<String, Permission> {
return ret;
}
private final class ChildPermissionResolver implements CacheLoader<String, Map<String, Boolean>> {
private final class ChildPermissionResolver implements Function<String, Map<String, Boolean>> {
private final boolean value;
private ChildPermissionResolver(boolean value) {
@ -122,7 +118,7 @@ public final class LPPermissionMap extends ForwardingMap<String, Permission> {
}
@Override
public Map<String, Boolean> load(@NonNull String key) {
public Map<String, Boolean> apply(@NonNull String key) {
Map<String, Boolean> children = new HashMap<>();
resolveChildren(children, Collections.singletonMap(key, this.value), false);
children.remove(key, this.value);

View File

@ -13,8 +13,3 @@ include (
'nukkit',
'velocity'
)

View File

@ -43,7 +43,6 @@ import me.lucko.luckperms.common.sender.DummySender;
import me.lucko.luckperms.common.sender.Sender;
import me.lucko.luckperms.common.tasks.CacheHousekeepingTask;
import me.lucko.luckperms.common.tasks.ExpireTemporaryTask;
import me.lucko.luckperms.common.treeview.PermissionRegistry;
import me.lucko.luckperms.common.util.MoreFiles;
import me.lucko.luckperms.sponge.calculator.SpongeCalculatorFactory;
import me.lucko.luckperms.sponge.commands.SpongeMainCommand;

View File

@ -25,12 +25,11 @@
package me.lucko.luckperms.sponge.service;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import me.lucko.luckperms.common.context.ContextManager;
import me.lucko.luckperms.common.util.LoadingMap;
import me.lucko.luckperms.common.util.Predicates;
import me.lucko.luckperms.sponge.LPSpongePlugin;
import me.lucko.luckperms.sponge.context.SpongeProxiedContextCalculator;
@ -98,8 +97,7 @@ public class LuckPermsService implements LPPermissionService {
/**
* The loaded collections in this service
*/
private final LoadingCache<String, LPSubjectCollection> collections = Caffeine.newBuilder()
.build(s -> new PersistedCollection(this, s));
private final Map<String, LPSubjectCollection> collections = LoadingMap.of(s -> new PersistedCollection(this, s));
public LuckPermsService(LPSpongePlugin plugin) {
this.plugin = plugin;
@ -121,7 +119,7 @@ public class LuckPermsService implements LPPermissionService {
// load known collections
for (String identifier : this.storage.getSavedCollections()) {
if (this.collections.asMap().containsKey(identifier.toLowerCase())) {
if (this.collections.containsKey(identifier.toLowerCase())) {
continue;
}
@ -191,7 +189,7 @@ public class LuckPermsService implements LPPermissionService {
@Override
public ImmutableMap<String, LPSubjectCollection> getLoadedCollections() {
return ImmutableMap.copyOf(this.collections.asMap());
return ImmutableMap.copyOf(this.collections);
}
@Override
@ -232,7 +230,7 @@ public class LuckPermsService implements LPPermissionService {
@Override
public void invalidateAllCaches() {
for (LPSubjectCollection collection : this.collections.asMap().values()) {
for (LPSubjectCollection collection : this.collections.values()) {
for (LPSubject subject : collection.getLoadedSubjects()) {
subject.invalidateCaches();
}

View File

@ -25,8 +25,6 @@
package me.lucko.luckperms.sponge.service.model.persisted;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@ -35,6 +33,7 @@ import com.google.common.collect.ImmutableSet;
import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.common.util.ImmutableCollectors;
import me.lucko.luckperms.common.util.LoadingMap;
import me.lucko.luckperms.common.util.Predicates;
import me.lucko.luckperms.sponge.service.LuckPermsService;
import me.lucko.luckperms.sponge.service.ProxyFactory;
@ -75,8 +74,7 @@ public class PersistedCollection implements LPSubjectCollection {
/**
* The contained subjects
*/
private final LoadingCache<String, PersistedSubject> subjects = Caffeine.newBuilder()
.build(s -> new PersistedSubject(getService(), this, s));
private final Map<String, PersistedSubject> subjects = LoadingMap.of(s -> new PersistedSubject(getService(), this, s));
public PersistedCollection(LuckPermsService service, String identifier) {
this.service = service;
@ -138,7 +136,7 @@ public class PersistedCollection implements LPSubjectCollection {
@Override
public CompletableFuture<Boolean> hasRegistered(String identifier) {
return CompletableFuture.completedFuture(this.subjects.asMap().containsKey(identifier.toLowerCase()));
return CompletableFuture.completedFuture(this.subjects.containsKey(identifier.toLowerCase()));
}
@Override
@ -152,12 +150,12 @@ public class PersistedCollection implements LPSubjectCollection {
@Override
public ImmutableCollection<LPSubject> getLoadedSubjects() {
return ImmutableList.copyOf(this.subjects.asMap().values());
return ImmutableList.copyOf(this.subjects.values());
}
@Override
public CompletableFuture<ImmutableSet<String>> getAllIdentifiers() {
return CompletableFuture.completedFuture(ImmutableSet.copyOf(this.subjects.asMap().keySet()));
return CompletableFuture.completedFuture(ImmutableSet.copyOf(this.subjects.keySet()));
}
@Override
@ -175,7 +173,7 @@ public class PersistedCollection implements LPSubjectCollection {
@Override
public ImmutableMap<LPSubject, Boolean> getLoadedWithPermission(String permission) {
ImmutableMap.Builder<LPSubject, Boolean> m = ImmutableMap.builder();
for (LPSubject subject : this.subjects.asMap().values()) {
for (LPSubject subject : this.subjects.values()) {
Tristate ts = subject.getPermissionValue(ImmutableContextSet.empty(), permission);
if (ts != Tristate.UNDEFINED) {
m.put(subject, ts.asBoolean());
@ -188,7 +186,7 @@ public class PersistedCollection implements LPSubjectCollection {
@Override
public ImmutableMap<LPSubject, Boolean> getLoadedWithPermission(ImmutableContextSet contexts, String permission) {
ImmutableMap.Builder<LPSubject, Boolean> m = ImmutableMap.builder();
for (LPSubject subject : this.subjects.asMap().values()) {
for (LPSubject subject : this.subjects.values()) {
Tristate ts = subject.getPermissionValue(contexts, permission);
if (ts != Tristate.UNDEFINED) {
m.put(subject, ts.asBoolean());