mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-24 03:25:19 +01:00
Simplify ExpiringSet implementation (#3969)
This commit is contained in:
parent
9d80de5d5c
commit
c773126a0f
@ -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<UUID> playerRateLimit = new ExpiringSet<>(500, TimeUnit.MILLISECONDS);
|
||||
private final Set<UUID> playerRateLimit = ExpiringSet.newExpiringSet(500, TimeUnit.MILLISECONDS);
|
||||
private final TabCompletions tabCompletions;
|
||||
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 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<UUID> receivedMessages;
|
||||
private final Set<UUID> 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);
|
||||
}
|
||||
|
||||
|
@ -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<UUID> recentlyUsed;
|
||||
private final Set<UUID> recentlyUsed;
|
||||
|
||||
// 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) {
|
||||
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
|
||||
|
@ -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<String> recentlyModifiedFiles = new ExpiringSet<>(4, TimeUnit.SECONDS);
|
||||
private final Set<String> recentlyModifiedFiles = ExpiringSet.newExpiringSet(4, TimeUnit.SECONDS);
|
||||
|
||||
/** The listener callback functions */
|
||||
private final List<Consumer<Path>> callbacks = new CopyOnWriteArrayList<>();
|
||||
|
@ -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 <E> element type
|
||||
*/
|
||||
public class ExpiringSet<E> {
|
||||
private final Cache<E, Long> 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 <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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user