mirror of
https://github.com/lucko/LuckPerms.git
synced 2025-08-26 15:54:22 +02:00
Simplify ExpiringSet implementation (#3969)
This commit is contained in:
@@ -79,6 +79,7 @@ import java.util.Collections;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
@@ -101,7 +102,7 @@ public class CommandManager {
|
|||||||
.build()
|
.build()
|
||||||
);
|
);
|
||||||
private final AtomicBoolean executingCommand = new AtomicBoolean(false);
|
private final AtomicBoolean executingCommand = new AtomicBoolean(false);
|
||||||
private final ExpiringSet<UUID> playerRateLimit = new ExpiringSet<>(500, TimeUnit.MILLISECONDS);
|
private final Set<UUID> playerRateLimit = ExpiringSet.newExpiringSet(500, TimeUnit.MILLISECONDS);
|
||||||
private final TabCompletions tabCompletions;
|
private final TabCompletions tabCompletions;
|
||||||
private final Map<String, Command<?>> mainCommands;
|
private final Map<String, Command<?>> mainCommands;
|
||||||
|
|
||||||
|
@@ -54,13 +54,14 @@ import org.checkerframework.checker.nullness.qual.NonNull;
|
|||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class LuckPermsMessagingService extends AsyncInterface implements InternalMessagingService, IncomingMessageConsumer {
|
public class LuckPermsMessagingService extends AsyncInterface implements InternalMessagingService, IncomingMessageConsumer {
|
||||||
private final LuckPermsPlugin plugin;
|
private final LuckPermsPlugin plugin;
|
||||||
private final ExpiringSet<UUID> receivedMessages;
|
private final Set<UUID> receivedMessages;
|
||||||
private final PushUpdateBuffer updateBuffer;
|
private final PushUpdateBuffer updateBuffer;
|
||||||
|
|
||||||
private final MessengerProvider messengerProvider;
|
private final MessengerProvider messengerProvider;
|
||||||
@@ -74,7 +75,7 @@ public class LuckPermsMessagingService extends AsyncInterface implements Interna
|
|||||||
this.messenger = messengerProvider.obtain(this);
|
this.messenger = messengerProvider.obtain(this);
|
||||||
Objects.requireNonNull(this.messenger, "messenger");
|
Objects.requireNonNull(this.messenger, "messenger");
|
||||||
|
|
||||||
this.receivedMessages = new ExpiringSet<>(5, TimeUnit.MINUTES);
|
this.receivedMessages = ExpiringSet.newExpiringSet(5, TimeUnit.MINUTES);
|
||||||
this.updateBuffer = new PushUpdateBuffer(plugin);
|
this.updateBuffer = new PushUpdateBuffer(plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -30,6 +30,7 @@ import me.lucko.luckperms.common.model.User;
|
|||||||
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
||||||
import me.lucko.luckperms.common.util.ExpiringSet;
|
import me.lucko.luckperms.common.util.ExpiringSet;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@@ -41,16 +42,16 @@ public class UserHousekeeper implements Runnable {
|
|||||||
private final UserManager<?> userManager;
|
private final UserManager<?> userManager;
|
||||||
|
|
||||||
// contains the uuids of users who have recently logged in / out
|
// contains the uuids of users who have recently logged in / out
|
||||||
private final ExpiringSet<UUID> recentlyUsed;
|
private final Set<UUID> recentlyUsed;
|
||||||
|
|
||||||
// contains the uuids of users who have recently been retrieved from the API
|
// contains the uuids of users who have recently been retrieved from the API
|
||||||
private final ExpiringSet<UUID> recentlyUsedApi;
|
private final Set<UUID> recentlyUsedApi;
|
||||||
|
|
||||||
public UserHousekeeper(LuckPermsPlugin plugin, UserManager<?> userManager, TimeoutSettings timeoutSettings) {
|
public UserHousekeeper(LuckPermsPlugin plugin, UserManager<?> userManager, TimeoutSettings timeoutSettings) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.userManager = userManager;
|
this.userManager = userManager;
|
||||||
this.recentlyUsed = new ExpiringSet<>(timeoutSettings.duration, timeoutSettings.unit);
|
this.recentlyUsed = ExpiringSet.newExpiringSet(timeoutSettings.duration, timeoutSettings.unit);
|
||||||
this.recentlyUsedApi = new ExpiringSet<>(5, TimeUnit.MINUTES);
|
this.recentlyUsedApi = ExpiringSet.newExpiringSet(5, TimeUnit.MINUTES);
|
||||||
}
|
}
|
||||||
|
|
||||||
// called when a player attempts a connection or logs out
|
// called when a player attempts a connection or logs out
|
||||||
|
@@ -36,6 +36,7 @@ import java.util.Collections;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
@@ -97,7 +98,7 @@ public class FileWatcher extends AbstractFileWatcher {
|
|||||||
private final Path path;
|
private final Path path;
|
||||||
|
|
||||||
/** A set of files which have been modified recently */
|
/** A set of files which have been modified recently */
|
||||||
private final ExpiringSet<String> recentlyModifiedFiles = new ExpiringSet<>(4, TimeUnit.SECONDS);
|
private final Set<String> recentlyModifiedFiles = ExpiringSet.newExpiringSet(4, TimeUnit.SECONDS);
|
||||||
|
|
||||||
/** The listener callback functions */
|
/** The listener callback functions */
|
||||||
private final List<Consumer<Path>> callbacks = new CopyOnWriteArrayList<>();
|
private final List<Consumer<Path>> callbacks = new CopyOnWriteArrayList<>();
|
||||||
|
@@ -25,36 +25,21 @@
|
|||||||
|
|
||||||
package me.lucko.luckperms.common.util;
|
package me.lucko.luckperms.common.util;
|
||||||
|
|
||||||
import com.github.benmanes.caffeine.cache.Cache;
|
import java.util.Collections;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
public final class ExpiringSet {
|
||||||
* A simple expiring set implementation using Caffeine caches
|
private ExpiringSet() {}
|
||||||
*
|
|
||||||
* @param <E> element type
|
|
||||||
*/
|
|
||||||
public class ExpiringSet<E> {
|
|
||||||
private final Cache<E, Long> cache;
|
|
||||||
private final long lifetime;
|
|
||||||
|
|
||||||
public ExpiringSet(long duration, TimeUnit unit) {
|
/**
|
||||||
this.cache = CaffeineFactory.newBuilder().expireAfterWrite(duration, unit).build();
|
* An expiring set using Caffeine caches
|
||||||
this.lifetime = unit.toMillis(duration);
|
*
|
||||||
|
* @param <E> the element type
|
||||||
|
* @return a new expiring set
|
||||||
|
*/
|
||||||
|
public static <E> Set<E> newExpiringSet(long duration, TimeUnit unit) {
|
||||||
|
return Collections.newSetFromMap(CaffeineFactory.newBuilder().expireAfterWrite(duration, unit).<E, Boolean>build().asMap());
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean add(E item) {
|
|
||||||
boolean present = contains(item);
|
|
||||||
this.cache.put(item, System.currentTimeMillis() + this.lifetime);
|
|
||||||
return !present;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean contains(E item) {
|
|
||||||
Long timeout = this.cache.getIfPresent(item);
|
|
||||||
return timeout != null && timeout > System.currentTimeMillis();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void remove(E item) {
|
|
||||||
this.cache.invalidate(item);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user