mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-04 14:38:49 +00:00
clean up caching user managers
This commit is contained in:
4 files changed
+22
-47
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();
|
||||
|
||||
+1
-15
@@ -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,11 +100,7 @@ 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(() -> {
|
||||
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)
|
||||
@@ -140,15 +135,6 @@ public final class MySQLUserManager extends DatabaseUserManager {
|
||||
});
|
||||
return carbonPlayerCommon.get();
|
||||
});
|
||||
}, this.executor);
|
||||
|
||||
this.attachPostLoad(uuid, future);
|
||||
|
||||
return future;
|
||||
});
|
||||
} finally {
|
||||
this.cacheLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+1
-15
@@ -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,11 +97,7 @@ 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(() -> {
|
||||
protected CarbonPlayerCommon loadOrCreate(final UUID uuid) {
|
||||
return this.jdbi.withHandle(handle -> {
|
||||
try {
|
||||
final @Nullable CarbonPlayerCommon carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
|
||||
@@ -141,15 +136,6 @@ public final class PostgreSQLUserManager extends DatabaseUserManager {
|
||||
return player;
|
||||
}
|
||||
});
|
||||
}, this.executor);
|
||||
|
||||
this.attachPostLoad(uuid, future);
|
||||
|
||||
return future;
|
||||
});
|
||||
} finally {
|
||||
this.cacheLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -78,11 +78,7 @@ 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(() -> {
|
||||
protected CarbonPlayerCommon loadOrCreate(final UUID uuid) {
|
||||
final Path userFile = this.userFile(uuid);
|
||||
|
||||
if (Files.exists(userFile)) {
|
||||
@@ -107,15 +103,6 @@ public class JSONUserManager extends CachingUserManager {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
private Path userFile(final UUID id) {
|
||||
|
||||
Reference in new issue
Block a user