mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 06:28:54 +00:00
clean up caching user managers
This commit is contained in:
4 files changed
+104
-129
No files matched your search
@@ -40,8 +40,8 @@ public abstract class CachingUserManager implements UserManagerInternal<CarbonPl
|
|||||||
|
|
||||||
protected final Logger logger;
|
protected final Logger logger;
|
||||||
protected final ExecutorService executor;
|
protected final ExecutorService executor;
|
||||||
protected final ReentrantLock cacheLock;
|
private final ReentrantLock cacheLock;
|
||||||
protected final Map<UUID, CompletableFuture<CarbonPlayerCommon>> cache;
|
private final Map<UUID, CompletableFuture<CarbonPlayerCommon>> cache;
|
||||||
|
|
||||||
protected CachingUserManager(final Logger logger, final ExecutorService executorService) {
|
protected CachingUserManager(final Logger logger, final ExecutorService executorService) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
@@ -50,6 +50,22 @@ public abstract class CachingUserManager implements UserManagerInternal<CarbonPl
|
|||||||
this.cache = new HashMap<>();
|
this.cache = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected abstract CarbonPlayerCommon loadOrCreate(UUID uuid);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
|
||||||
|
this.cacheLock.lock();
|
||||||
|
try {
|
||||||
|
return this.cache.computeIfAbsent(uuid, $ -> {
|
||||||
|
final CompletableFuture<CarbonPlayerCommon> future = CompletableFuture.supplyAsync(() -> this.loadOrCreate(uuid), this.executor);
|
||||||
|
this.attachPostLoad(uuid, future);
|
||||||
|
return future;
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
this.cacheLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void shutdown() {
|
public void shutdown() {
|
||||||
this.cacheLock.lock();
|
this.cacheLock.lock();
|
||||||
@@ -105,7 +121,7 @@ public abstract class CachingUserManager implements UserManagerInternal<CarbonPl
|
|||||||
|
|
||||||
// Don't keep failed requests, so they can be retried on the next request
|
// Don't keep failed requests, so they can be retried on the next request
|
||||||
// The caller is expected to handle the error
|
// The caller is expected to handle the error
|
||||||
protected void attachPostLoad(final UUID uuid, final CompletableFuture<CarbonPlayerCommon> future) {
|
private void attachPostLoad(final UUID uuid, final CompletableFuture<CarbonPlayerCommon> future) {
|
||||||
future.whenComplete((result, thr) -> {
|
future.whenComplete((result, thr) -> {
|
||||||
if (result == null || thr != null) {
|
if (result == null || thr != null) {
|
||||||
this.cacheLock.lock();
|
this.cacheLock.lock();
|
||||||
|
|||||||
+31
-45
@@ -24,7 +24,6 @@ import com.zaxxer.hikari.HikariDataSource;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import net.draycia.carbon.api.CarbonChat;
|
import net.draycia.carbon.api.CarbonChat;
|
||||||
import net.draycia.carbon.api.CarbonChatProvider;
|
import net.draycia.carbon.api.CarbonChatProvider;
|
||||||
@@ -101,54 +100,41 @@ public final class MySQLUserManager extends DatabaseUserManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
|
protected CarbonPlayerCommon loadOrCreate(final UUID uuid) {
|
||||||
this.cacheLock.lock();
|
return this.jdbi.withHandle(handle -> {
|
||||||
try {
|
final Optional<CarbonPlayerCommon> carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
|
||||||
return this.cache.computeIfAbsent(uuid, $ -> {
|
.bind("id", uuid)
|
||||||
final CompletableFuture<CarbonPlayerCommon> future = CompletableFuture.supplyAsync(() -> {
|
.mapTo(CarbonPlayerCommon.class)
|
||||||
return this.jdbi.withHandle(handle -> {
|
.findOne();
|
||||||
final Optional<CarbonPlayerCommon> carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
|
|
||||||
.bind("id", uuid)
|
|
||||||
.mapTo(CarbonPlayerCommon.class)
|
|
||||||
.findOne();
|
|
||||||
|
|
||||||
if (carbonPlayerCommon.isEmpty()) {
|
if (carbonPlayerCommon.isEmpty()) {
|
||||||
// Player doesn't exist in the DB, create them!
|
// Player doesn't exist in the DB, create them!
|
||||||
final String name = Objects.requireNonNull(this.profileResolver.resolveName(uuid).join());
|
final String name = Objects.requireNonNull(this.profileResolver.resolveName(uuid).join());
|
||||||
final CarbonPlayerCommon player = new CarbonPlayerCommon(name, uuid);
|
final CarbonPlayerCommon player = new CarbonPlayerCommon(name, uuid);
|
||||||
|
|
||||||
this.bindPlayerArguments(handle.createUpdate(this.locator.query("insert-player")), player).execute();
|
this.bindPlayerArguments(handle.createUpdate(this.locator.query("insert-player")), player).execute();
|
||||||
|
|
||||||
return player;
|
return player;
|
||||||
}
|
}
|
||||||
|
|
||||||
handle.createQuery(this.locator.query("select-ignores"))
|
handle.createQuery(this.locator.query("select-ignores"))
|
||||||
.bind("id", uuid)
|
.bind("id", uuid)
|
||||||
.mapTo(UUID.class)
|
.mapTo(UUID.class)
|
||||||
.forEach(ignoredPlayer -> carbonPlayerCommon.get().ignoredPlayers().add(ignoredPlayer));
|
.forEach(ignoredPlayer -> carbonPlayerCommon.get().ignoredPlayers().add(ignoredPlayer));
|
||||||
handle.createQuery(this.locator.query("select-leftchannels"))
|
handle.createQuery(this.locator.query("select-leftchannels"))
|
||||||
.bind("id", uuid)
|
.bind("id", uuid)
|
||||||
.mapTo(Key.class)
|
.mapTo(Key.class)
|
||||||
.forEach(channel -> {
|
.forEach(channel -> {
|
||||||
final @Nullable ChatChannel chatChannel = CarbonChatProvider.carbonChat()
|
final @Nullable ChatChannel chatChannel = CarbonChatProvider.carbonChat()
|
||||||
.channelRegistry()
|
.channelRegistry()
|
||||||
.get(channel);
|
.get(channel);
|
||||||
if (chatChannel == null) {
|
if (chatChannel == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
carbonPlayerCommon.get().leftChannels().add(channel);
|
carbonPlayerCommon.get().leftChannels().add(channel);
|
||||||
});
|
});
|
||||||
return carbonPlayerCommon.get();
|
return carbonPlayerCommon.get();
|
||||||
});
|
});
|
||||||
}, this.executor);
|
|
||||||
|
|
||||||
this.attachPostLoad(uuid, future);
|
|
||||||
|
|
||||||
return future;
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
this.cacheLock.unlock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
+33
-47
@@ -23,7 +23,6 @@ import com.zaxxer.hikari.HikariConfig;
|
|||||||
import com.zaxxer.hikari.HikariDataSource;
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import net.draycia.carbon.api.CarbonChat;
|
import net.draycia.carbon.api.CarbonChat;
|
||||||
import net.draycia.carbon.api.CarbonChatProvider;
|
import net.draycia.carbon.api.CarbonChatProvider;
|
||||||
@@ -98,58 +97,45 @@ public final class PostgreSQLUserManager extends DatabaseUserManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
|
protected CarbonPlayerCommon loadOrCreate(final UUID uuid) {
|
||||||
this.cacheLock.lock();
|
return this.jdbi.withHandle(handle -> {
|
||||||
try {
|
try {
|
||||||
return this.cache.computeIfAbsent(uuid, $ -> {
|
final @Nullable CarbonPlayerCommon carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
|
||||||
final CompletableFuture<CarbonPlayerCommon> future = CompletableFuture.supplyAsync(() -> {
|
.bind("id", uuid)
|
||||||
return this.jdbi.withHandle(handle -> {
|
.mapTo(CarbonPlayerCommon.class)
|
||||||
try {
|
.first();
|
||||||
final @Nullable CarbonPlayerCommon carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
|
|
||||||
.bind("id", uuid)
|
|
||||||
.mapTo(CarbonPlayerCommon.class)
|
|
||||||
.first();
|
|
||||||
|
|
||||||
handle.createQuery(this.locator.query("select-ignores"))
|
handle.createQuery(this.locator.query("select-ignores"))
|
||||||
.bind("id", uuid)
|
.bind("id", uuid)
|
||||||
.mapTo(UUID.class)
|
.mapTo(UUID.class)
|
||||||
.forEach(ignoredPlayer -> carbonPlayerCommon.ignoring(ignoredPlayer, true));
|
.forEach(ignoredPlayer -> carbonPlayerCommon.ignoring(ignoredPlayer, true));
|
||||||
|
|
||||||
handle.createQuery(this.locator.query("select-leftchannels"))
|
handle.createQuery(this.locator.query("select-leftchannels"))
|
||||||
.bind("id", uuid)
|
.bind("id", uuid)
|
||||||
.mapTo(Key.class)
|
.mapTo(Key.class)
|
||||||
.forEach(channel -> {
|
.forEach(channel -> {
|
||||||
final @Nullable ChatChannel chatChannel = CarbonChatProvider.carbonChat()
|
final @Nullable ChatChannel chatChannel = CarbonChatProvider.carbonChat()
|
||||||
.channelRegistry()
|
.channelRegistry()
|
||||||
.get(channel);
|
.get(channel);
|
||||||
if (chatChannel == null) {
|
if (chatChannel == null) {
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
carbonPlayerCommon.leftChannels().add(channel);
|
|
||||||
});
|
|
||||||
return carbonPlayerCommon;
|
|
||||||
} catch (final IllegalStateException exception) {
|
|
||||||
// Player doesn't exist in the DB, create them!
|
|
||||||
final String name = Objects.requireNonNull(
|
|
||||||
this.profileResolver.resolveName(uuid).join());
|
|
||||||
|
|
||||||
final CarbonPlayerCommon player = new CarbonPlayerCommon(name, uuid);
|
|
||||||
|
|
||||||
this.bindPlayerArguments(handle.createUpdate(this.locator.query("insert-player")), player)
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
return player;
|
|
||||||
}
|
}
|
||||||
|
carbonPlayerCommon.leftChannels().add(channel);
|
||||||
});
|
});
|
||||||
}, this.executor);
|
return carbonPlayerCommon;
|
||||||
|
} catch (final IllegalStateException exception) {
|
||||||
|
// Player doesn't exist in the DB, create them!
|
||||||
|
final String name = Objects.requireNonNull(
|
||||||
|
this.profileResolver.resolveName(uuid).join());
|
||||||
|
|
||||||
this.attachPostLoad(uuid, future);
|
final CarbonPlayerCommon player = new CarbonPlayerCommon(name, uuid);
|
||||||
|
|
||||||
return future;
|
this.bindPlayerArguments(handle.createUpdate(this.locator.query("insert-player")), player)
|
||||||
});
|
.execute();
|
||||||
} finally {
|
|
||||||
this.cacheLock.unlock();
|
return player;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -78,44 +78,31 @@ public class JSONUserManager extends CachingUserManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
|
protected CarbonPlayerCommon loadOrCreate(final UUID uuid) {
|
||||||
this.cacheLock.lock();
|
final Path userFile = this.userFile(uuid);
|
||||||
try {
|
|
||||||
return this.cache.computeIfAbsent(uuid, $ -> {
|
|
||||||
final CompletableFuture<CarbonPlayerCommon> future = CompletableFuture.supplyAsync(() -> {
|
|
||||||
final Path userFile = this.userFile(uuid);
|
|
||||||
|
|
||||||
if (Files.exists(userFile)) {
|
if (Files.exists(userFile)) {
|
||||||
try {
|
try {
|
||||||
final @Nullable CarbonPlayerCommon player =
|
final @Nullable CarbonPlayerCommon player =
|
||||||
this.serializer.fromJson(Files.newBufferedReader(userFile), CarbonPlayerCommon.class);
|
this.serializer.fromJson(Files.newBufferedReader(userFile), CarbonPlayerCommon.class);
|
||||||
|
|
||||||
if (player == null) {
|
if (player == null) {
|
||||||
throw new IllegalStateException("Player file found but was empty.");
|
throw new IllegalStateException("Player file found but was empty.");
|
||||||
}
|
}
|
||||||
player.profileResolver = this.profileResolver;
|
player.profileResolver = this.profileResolver;
|
||||||
player.leftChannels().removeIf(channel -> CarbonChatProvider.carbonChat()
|
player.leftChannels().removeIf(channel -> CarbonChatProvider.carbonChat()
|
||||||
.channelRegistry()
|
.channelRegistry()
|
||||||
.get(channel) == null);
|
.get(channel) == null);
|
||||||
|
|
||||||
return player;
|
return player;
|
||||||
} catch (final IOException exception) {
|
} catch (final IOException exception) {
|
||||||
throw new RuntimeException(exception);
|
throw new RuntimeException(exception);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
final CarbonPlayerCommon player = new CarbonPlayerCommon(null /* Username will be resolved when requested */, uuid);
|
|
||||||
player.profileResolver = this.profileResolver;
|
|
||||||
return player;
|
|
||||||
}, this.executor);
|
|
||||||
|
|
||||||
this.attachPostLoad(uuid, future);
|
|
||||||
|
|
||||||
return future;
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
this.cacheLock.unlock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final CarbonPlayerCommon player = new CarbonPlayerCommon(null /* Username will be resolved when requested */, uuid);
|
||||||
|
player.profileResolver = this.profileResolver;
|
||||||
|
return player;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Path userFile(final UUID id) {
|
private Path userFile(final UUID id) {
|
||||||
|
|||||||
Reference in new issue
Block a user