don't synch on get/put

This commit is contained in:
Draycia committed 2021-12-01 13:43:35 -08:00
1 parent 9ea76d8a4e
commit 294ddeaa23
1 file changed
+4 -15
@@ -33,7 +33,6 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import net.draycia.carbon.api.CarbonChat; import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.ComponentPlayerResult; import net.draycia.carbon.api.users.ComponentPlayerResult;
@@ -84,11 +83,7 @@ public class JSONUserManager implements UserManager<CarbonPlayerCommon> {
@Override @Override
public CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> carbonPlayer(final UUID uuid) { public CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> carbonPlayer(final UUID uuid) {
return CompletableFuture.supplyAsync(() -> { return CompletableFuture.supplyAsync(() -> {
final @Nullable CarbonPlayerCommon cachedPlayer; final @Nullable CarbonPlayerCommon cachedPlayer = this.userCache.get(uuid);
synchronized (this.userCache) {
cachedPlayer = this.userCache.get(uuid);
}
if (cachedPlayer != null) { if (cachedPlayer != null) {
return new ComponentPlayerResult<>(cachedPlayer, empty()); return new ComponentPlayerResult<>(cachedPlayer, empty());
@@ -105,9 +100,7 @@ public class JSONUserManager implements UserManager<CarbonPlayerCommon> {
return new ComponentPlayerResult<>(null, text("Player file found but was empty.")); return new ComponentPlayerResult<>(null, text("Player file found but was empty."));
} }
synchronized (this.userCache) { this.userCache.put(uuid, player);
this.userCache.put(uuid, player);
}
return new ComponentPlayerResult<>(player, empty()); return new ComponentPlayerResult<>(player, empty());
} catch (final IOException exception) { } catch (final IOException exception) {
@@ -120,9 +113,7 @@ public class JSONUserManager implements UserManager<CarbonPlayerCommon> {
final CarbonPlayerCommon player = new CarbonPlayerCommon(name, uuid); final CarbonPlayerCommon player = new CarbonPlayerCommon(name, uuid);
synchronized (this.userCache) { this.userCache.put(uuid, player);
this.userCache.put(uuid, player);
}
return new ComponentPlayerResult<>(player, empty()); return new ComponentPlayerResult<>(player, empty());
}); });
@@ -161,9 +152,7 @@ public class JSONUserManager implements UserManager<CarbonPlayerCommon> {
@Override @Override
public CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> saveAndInvalidatePlayer(final CarbonPlayerCommon player) { public CompletableFuture<ComponentPlayerResult<CarbonPlayerCommon>> saveAndInvalidatePlayer(final CarbonPlayerCommon player) {
return this.savePlayer(player).thenApply(result -> { return this.savePlayer(player).thenApply(result -> {
synchronized (this.userCache) { this.userCache.remove(player.uuid());
this.userCache.remove(player.uuid());
}
return result; return result;
}); });