diff --git a/common/src/main/java/me/lucko/luckperms/common/command/CommandManager.java b/common/src/main/java/me/lucko/luckperms/common/command/CommandManager.java index 51cb7a2a1..6fd381183 100644 --- a/common/src/main/java/me/lucko/luckperms/common/command/CommandManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/command/CommandManager.java @@ -79,6 +79,7 @@ import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Set; import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; @@ -101,7 +102,7 @@ public class CommandManager { .build() ); private final AtomicBoolean executingCommand = new AtomicBoolean(false); - private final ExpiringSet playerRateLimit = new ExpiringSet<>(500, TimeUnit.MILLISECONDS); + private final Set playerRateLimit = ExpiringSet.newExpiringSet(500, TimeUnit.MILLISECONDS); private final TabCompletions tabCompletions; private final Map> mainCommands; diff --git a/common/src/main/java/me/lucko/luckperms/common/messaging/LuckPermsMessagingService.java b/common/src/main/java/me/lucko/luckperms/common/messaging/LuckPermsMessagingService.java index 508fb8906..4322c3204 100644 --- a/common/src/main/java/me/lucko/luckperms/common/messaging/LuckPermsMessagingService.java +++ b/common/src/main/java/me/lucko/luckperms/common/messaging/LuckPermsMessagingService.java @@ -54,13 +54,14 @@ import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import java.util.Objects; +import java.util.Set; import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; public class LuckPermsMessagingService extends AsyncInterface implements InternalMessagingService, IncomingMessageConsumer { private final LuckPermsPlugin plugin; - private final ExpiringSet receivedMessages; + private final Set receivedMessages; private final PushUpdateBuffer updateBuffer; private final MessengerProvider messengerProvider; @@ -74,7 +75,7 @@ public class LuckPermsMessagingService extends AsyncInterface implements Interna this.messenger = messengerProvider.obtain(this); Objects.requireNonNull(this.messenger, "messenger"); - this.receivedMessages = new ExpiringSet<>(5, TimeUnit.MINUTES); + this.receivedMessages = ExpiringSet.newExpiringSet(5, TimeUnit.MINUTES); this.updateBuffer = new PushUpdateBuffer(plugin); } diff --git a/common/src/main/java/me/lucko/luckperms/common/model/manager/user/UserHousekeeper.java b/common/src/main/java/me/lucko/luckperms/common/model/manager/user/UserHousekeeper.java index f1ae13665..c458d2440 100644 --- a/common/src/main/java/me/lucko/luckperms/common/model/manager/user/UserHousekeeper.java +++ b/common/src/main/java/me/lucko/luckperms/common/model/manager/user/UserHousekeeper.java @@ -30,6 +30,7 @@ import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.util.ExpiringSet; +import java.util.Set; import java.util.UUID; import java.util.concurrent.TimeUnit; @@ -41,16 +42,16 @@ public class UserHousekeeper implements Runnable { private final UserManager userManager; // contains the uuids of users who have recently logged in / out - private final ExpiringSet recentlyUsed; + private final Set recentlyUsed; // contains the uuids of users who have recently been retrieved from the API - private final ExpiringSet recentlyUsedApi; + private final Set recentlyUsedApi; public UserHousekeeper(LuckPermsPlugin plugin, UserManager userManager, TimeoutSettings timeoutSettings) { this.plugin = plugin; this.userManager = userManager; - this.recentlyUsed = new ExpiringSet<>(timeoutSettings.duration, timeoutSettings.unit); - this.recentlyUsedApi = new ExpiringSet<>(5, TimeUnit.MINUTES); + this.recentlyUsed = ExpiringSet.newExpiringSet(timeoutSettings.duration, timeoutSettings.unit); + this.recentlyUsedApi = ExpiringSet.newExpiringSet(5, TimeUnit.MINUTES); } // called when a player attempts a connection or logs out diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/file/watcher/FileWatcher.java b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/file/watcher/FileWatcher.java index 2aaddcd67..c5172e9c2 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/file/watcher/FileWatcher.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/file/watcher/FileWatcher.java @@ -36,6 +36,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; @@ -97,7 +98,7 @@ public class FileWatcher extends AbstractFileWatcher { private final Path path; /** A set of files which have been modified recently */ - private final ExpiringSet recentlyModifiedFiles = new ExpiringSet<>(4, TimeUnit.SECONDS); + private final Set recentlyModifiedFiles = ExpiringSet.newExpiringSet(4, TimeUnit.SECONDS); /** The listener callback functions */ private final List> callbacks = new CopyOnWriteArrayList<>(); diff --git a/common/src/main/java/me/lucko/luckperms/common/util/ExpiringSet.java b/common/src/main/java/me/lucko/luckperms/common/util/ExpiringSet.java index 8ed2535d9..d4e12d5c3 100644 --- a/common/src/main/java/me/lucko/luckperms/common/util/ExpiringSet.java +++ b/common/src/main/java/me/lucko/luckperms/common/util/ExpiringSet.java @@ -25,36 +25,21 @@ 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; -/** - * A simple expiring set implementation using Caffeine caches - * - * @param element type - */ -public class ExpiringSet { - private final Cache cache; - private final long lifetime; +public final class ExpiringSet { + private ExpiringSet() {} - public ExpiringSet(long duration, TimeUnit unit) { - this.cache = CaffeineFactory.newBuilder().expireAfterWrite(duration, unit).build(); - this.lifetime = unit.toMillis(duration); + /** + * An expiring set using Caffeine caches + * + * @param the element type + * @return a new expiring set + */ + public static Set newExpiringSet(long duration, TimeUnit unit) { + return Collections.newSetFromMap(CaffeineFactory.newBuilder().expireAfterWrite(duration, unit).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); - } }