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
Unverified
parent de24817d9c
commit 4b1cf51530
17 changed files with 83 additions and 124 deletions
@@ -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);
}
@@ -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);
}
@@ -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);
@@ -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) {
@@ -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;
@@ -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.
@@ -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);
@@ -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);