Add/remove linked accounts from cache when they are created/removed manually

This commit is contained in:
Vankka 2024-06-29 15:44:04 +03:00
parent 916be86fbf
commit 3336cf0f85
No known key found for this signature in database
GPG Key ID: 62E48025ED4E7EBB
3 changed files with 39 additions and 7 deletions

View File

@ -22,6 +22,7 @@ import com.discordsrv.api.event.bus.Subscribe;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.event.events.player.PlayerConnectedEvent;
import com.discordsrv.common.linking.LinkProvider;
import com.discordsrv.common.linking.LinkStore;
import com.github.benmanes.caffeine.cache.AsyncCacheLoader;
import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
import com.github.benmanes.caffeine.cache.Cache;
@ -155,4 +156,38 @@ public abstract class CachedLinkProvider implements LinkProvider {
playerToUser.get(uuid);
linkingAllowed.remove(uuid);
}
protected void addToCache(UUID playerUUID, long userId) {
playerToUser.put(playerUUID, CompletableFuture.completedFuture(userId));
}
protected void evictFromCache(UUID playerUUID) {
playerToUser.synchronous().invalidate(playerUUID);
}
public static abstract class Store extends CachedLinkProvider implements LinkStore {
public Store(DiscordSRV discordSRV) {
super(discordSRV);
}
public abstract CompletableFuture<Void> link(@NotNull UUID playerUUID, long userId);
public abstract CompletableFuture<Void> unlink(@NotNull UUID playerUUID, long userId);
@Override
public final CompletableFuture<Void> createLink(@NotNull UUID playerUUID, long userId) {
return link(playerUUID, userId).thenApply(v -> {
addToCache(playerUUID, userId);
return null;
});
}
@Override
public CompletableFuture<Void> removeLink(@NotNull UUID playerUUID, long userId) {
return unlink(playerUUID, userId).thenApply(v -> {
evictFromCache(playerUUID);
return null;
});
}
}
}

View File

@ -22,7 +22,6 @@ import com.discordsrv.api.component.MinecraftComponent;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.function.CheckedSupplier;
import com.discordsrv.common.future.util.CompletableFutureUtil;
import com.discordsrv.common.linking.LinkProvider;
import com.discordsrv.common.linking.LinkStore;
import com.discordsrv.common.linking.LinkingModule;
import com.discordsrv.common.linking.requirelinking.RequiredLinkingModule;
@ -44,7 +43,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class MinecraftAuthenticationLinker extends CachedLinkProvider implements LinkProvider {
public class MinecraftAuthenticationLinker extends CachedLinkProvider {
public static final String BASE_LINK_URL = DiscordSRV.WEBSITE + "/link";

View File

@ -21,8 +21,6 @@ package com.discordsrv.common.linking.impl;
import com.discordsrv.api.component.MinecraftComponent;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.component.util.ComponentUtil;
import com.discordsrv.common.linking.LinkProvider;
import com.discordsrv.common.linking.LinkStore;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -33,7 +31,7 @@ import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
public class StorageLinker extends CachedLinkProvider implements LinkProvider, LinkStore {
public class StorageLinker extends CachedLinkProvider.Store {
public StorageLinker(DiscordSRV discordSRV) {
super(discordSRV);
@ -56,12 +54,12 @@ public class StorageLinker extends CachedLinkProvider implements LinkProvider, L
}
@Override
public CompletableFuture<Void> createLink(@NotNull UUID playerUUID, long userId) {
public CompletableFuture<Void> link(@NotNull UUID playerUUID, long userId) {
return discordSRV.scheduler().execute(() -> discordSRV.storage().createLink(playerUUID, userId));
}
@Override
public CompletableFuture<Void> removeLink(@NotNull UUID playerUUID, long userId) {
public CompletableFuture<Void> unlink(@NotNull UUID playerUUID, long userId) {
return discordSRV.scheduler().execute(() -> discordSRV.storage().removeLink(playerUUID, userId));
}