mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 22:15:45 +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 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();
|
||||||
|
|||||||
+1
-15
@@ -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,11 +100,7 @@ public final class MySQLUserManager extends DatabaseUserManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
|
protected CarbonPlayerCommon loadOrCreate(final UUID uuid) {
|
||||||
this.cacheLock.lock();
|
|
||||||
try {
|
|
||||||
return this.cache.computeIfAbsent(uuid, $ -> {
|
|
||||||
final CompletableFuture<CarbonPlayerCommon> future = CompletableFuture.supplyAsync(() -> {
|
|
||||||
return this.jdbi.withHandle(handle -> {
|
return this.jdbi.withHandle(handle -> {
|
||||||
final Optional<CarbonPlayerCommon> carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
|
final Optional<CarbonPlayerCommon> carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
|
||||||
.bind("id", uuid)
|
.bind("id", uuid)
|
||||||
@@ -140,15 +135,6 @@ public final class MySQLUserManager extends DatabaseUserManager {
|
|||||||
});
|
});
|
||||||
return carbonPlayerCommon.get();
|
return carbonPlayerCommon.get();
|
||||||
});
|
});
|
||||||
}, this.executor);
|
|
||||||
|
|
||||||
this.attachPostLoad(uuid, future);
|
|
||||||
|
|
||||||
return future;
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
this.cacheLock.unlock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
+1
-15
@@ -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,11 +97,7 @@ public final class PostgreSQLUserManager extends DatabaseUserManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
|
protected CarbonPlayerCommon loadOrCreate(final UUID uuid) {
|
||||||
this.cacheLock.lock();
|
|
||||||
try {
|
|
||||||
return this.cache.computeIfAbsent(uuid, $ -> {
|
|
||||||
final CompletableFuture<CarbonPlayerCommon> future = CompletableFuture.supplyAsync(() -> {
|
|
||||||
return this.jdbi.withHandle(handle -> {
|
return this.jdbi.withHandle(handle -> {
|
||||||
try {
|
try {
|
||||||
final @Nullable CarbonPlayerCommon carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
|
final @Nullable CarbonPlayerCommon carbonPlayerCommon = handle.createQuery(this.locator.query("select-player"))
|
||||||
@@ -141,15 +136,6 @@ public final class PostgreSQLUserManager extends DatabaseUserManager {
|
|||||||
return player;
|
return player;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, this.executor);
|
|
||||||
|
|
||||||
this.attachPostLoad(uuid, future);
|
|
||||||
|
|
||||||
return future;
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
this.cacheLock.unlock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -78,11 +78,7 @@ public class JSONUserManager extends CachingUserManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<CarbonPlayerCommon> user(final UUID uuid) {
|
protected CarbonPlayerCommon loadOrCreate(final UUID uuid) {
|
||||||
this.cacheLock.lock();
|
|
||||||
try {
|
|
||||||
return this.cache.computeIfAbsent(uuid, $ -> {
|
|
||||||
final CompletableFuture<CarbonPlayerCommon> future = CompletableFuture.supplyAsync(() -> {
|
|
||||||
final Path userFile = this.userFile(uuid);
|
final Path userFile = this.userFile(uuid);
|
||||||
|
|
||||||
if (Files.exists(userFile)) {
|
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);
|
final CarbonPlayerCommon player = new CarbonPlayerCommon(null /* Username will be resolved when requested */, uuid);
|
||||||
player.profileResolver = this.profileResolver;
|
player.profileResolver = this.profileResolver;
|
||||||
return player;
|
return player;
|
||||||
}, this.executor);
|
|
||||||
|
|
||||||
this.attachPostLoad(uuid, future);
|
|
||||||
|
|
||||||
return future;
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
this.cacheLock.unlock();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Path userFile(final UUID id) {
|
private Path userFile(final UUID id) {
|
||||||
|
|||||||
Reference in new issue
Block a user