clean up caching user managers

This commit is contained in:
Jason Penilla committed 2023-03-23 13:36:20 -07:00
1 parent 9f58e81d20
commit 3b7f70e1d3
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 ExecutorService executor;
protected final ReentrantLock cacheLock;
protected final Map<UUID, CompletableFuture<CarbonPlayerCommon>> cache;
private final ReentrantLock cacheLock;
private final Map<UUID, CompletableFuture<CarbonPlayerCommon>> cache;
protected CachingUserManager(final Logger logger, final ExecutorService executorService) {
this.logger = logger;
@@ -50,6 +50,22 @@ public abstract class CachingUserManager implements UserManagerInternal<CarbonPl
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
public void shutdown() {
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
// 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) -> {
if (result == null || thr != null) {
this.cacheLock.lock();
@@ -24,7 +24,6 @@ import com.zaxxer.hikari.HikariDataSource;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import javax.sql.DataSource;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.CarbonChatProvider;
@@ -101,54 +100,41 @@ public final class MySQLUserManager extends DatabaseUserManager {
}
@Override
public CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
this.cacheLock.lock();
try {
return this.cache.computeIfAbsent(uuid, $ -> {
final CompletableFuture<CarbonPlayerCommon> future = CompletableFuture.supplyAsync(() -> {
return this.jdbi.withHandle(handle -> {
final Optional<CarbonPlayerCommon> carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
.bind("id", uuid)
.mapTo(CarbonPlayerCommon.class)
.findOne();
protected CarbonPlayerCommon loadOrCreate(final UUID uuid) {
return this.jdbi.withHandle(handle -> {
final Optional<CarbonPlayerCommon> carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
.bind("id", uuid)
.mapTo(CarbonPlayerCommon.class)
.findOne();
if (carbonPlayerCommon.isEmpty()) {
// 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);
if (carbonPlayerCommon.isEmpty()) {
// 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();
this.bindPlayerArguments(handle.createUpdate(this.locator.query("insert-player")), player).execute();
return player;
}
return player;
}
handle.createQuery(this.locator.query("select-ignores"))
.bind("id", uuid)
.mapTo(UUID.class)
.forEach(ignoredPlayer -> carbonPlayerCommon.get().ignoredPlayers().add(ignoredPlayer));
handle.createQuery(this.locator.query("select-leftchannels"))
.bind("id", uuid)
.mapTo(Key.class)
.forEach(channel -> {
final @Nullable ChatChannel chatChannel = CarbonChatProvider.carbonChat()
.channelRegistry()
.get(channel);
if (chatChannel == null) {
return;
}
carbonPlayerCommon.get().leftChannels().add(channel);
});
return carbonPlayerCommon.get();
});
}, this.executor);
this.attachPostLoad(uuid, future);
return future;
});
} finally {
this.cacheLock.unlock();
}
handle.createQuery(this.locator.query("select-ignores"))
.bind("id", uuid)
.mapTo(UUID.class)
.forEach(ignoredPlayer -> carbonPlayerCommon.get().ignoredPlayers().add(ignoredPlayer));
handle.createQuery(this.locator.query("select-leftchannels"))
.bind("id", uuid)
.mapTo(Key.class)
.forEach(channel -> {
final @Nullable ChatChannel chatChannel = CarbonChatProvider.carbonChat()
.channelRegistry()
.get(channel);
if (chatChannel == null) {
return;
}
carbonPlayerCommon.get().leftChannels().add(channel);
});
return carbonPlayerCommon.get();
});
}
@Override
@@ -23,7 +23,6 @@ import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import javax.sql.DataSource;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.CarbonChatProvider;
@@ -98,58 +97,45 @@ public final class PostgreSQLUserManager extends DatabaseUserManager {
}
@Override
public CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
this.cacheLock.lock();
try {
return this.cache.computeIfAbsent(uuid, $ -> {
final CompletableFuture<CarbonPlayerCommon> future = CompletableFuture.supplyAsync(() -> {
return this.jdbi.withHandle(handle -> {
try {
final @Nullable CarbonPlayerCommon carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
.bind("id", uuid)
.mapTo(CarbonPlayerCommon.class)
.first();
protected CarbonPlayerCommon loadOrCreate(final UUID uuid) {
return this.jdbi.withHandle(handle -> {
try {
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"))
.bind("id", uuid)
.mapTo(UUID.class)
.forEach(ignoredPlayer -> carbonPlayerCommon.ignoring(ignoredPlayer, true));
handle.createQuery(this.locator.query("select-ignores"))
.bind("id", uuid)
.mapTo(UUID.class)
.forEach(ignoredPlayer -> carbonPlayerCommon.ignoring(ignoredPlayer, true));
handle.createQuery(this.locator.query("select-leftchannels"))
.bind("id", uuid)
.mapTo(Key.class)
.forEach(channel -> {
final @Nullable ChatChannel chatChannel = CarbonChatProvider.carbonChat()
.channelRegistry()
.get(channel);
if (chatChannel == null) {
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;
handle.createQuery(this.locator.query("select-leftchannels"))
.bind("id", uuid)
.mapTo(Key.class)
.forEach(channel -> {
final @Nullable ChatChannel chatChannel = CarbonChatProvider.carbonChat()
.channelRegistry()
.get(channel);
if (chatChannel == null) {
return;
}
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;
});
} finally {
this.cacheLock.unlock();
}
this.bindPlayerArguments(handle.createUpdate(this.locator.query("insert-player")), player)
.execute();
return player;
}
});
}
@Override
@@ -78,44 +78,31 @@ public class JSONUserManager extends CachingUserManager {
}
@Override
public CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
this.cacheLock.lock();
try {
return this.cache.computeIfAbsent(uuid, $ -> {
final CompletableFuture<CarbonPlayerCommon> future = CompletableFuture.supplyAsync(() -> {
final Path userFile = this.userFile(uuid);
protected CarbonPlayerCommon loadOrCreate(final UUID uuid) {
final Path userFile = this.userFile(uuid);
if (Files.exists(userFile)) {
try {
final @Nullable CarbonPlayerCommon player =
this.serializer.fromJson(Files.newBufferedReader(userFile), CarbonPlayerCommon.class);
if (Files.exists(userFile)) {
try {
final @Nullable CarbonPlayerCommon player =
this.serializer.fromJson(Files.newBufferedReader(userFile), CarbonPlayerCommon.class);
if (player == null) {
throw new IllegalStateException("Player file found but was empty.");
}
player.profileResolver = this.profileResolver;
player.leftChannels().removeIf(channel -> CarbonChatProvider.carbonChat()
.channelRegistry()
.get(channel) == null);
if (player == null) {
throw new IllegalStateException("Player file found but was empty.");
}
player.profileResolver = this.profileResolver;
player.leftChannels().removeIf(channel -> CarbonChatProvider.carbonChat()
.channelRegistry()
.get(channel) == null);
return player;
} catch (final IOException 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();
return player;
} catch (final IOException exception) {
throw new RuntimeException(exception);
}
}
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) {