From 32e3e2e5a2eb4b0f4fc023dca7b9482b7a97cedc Mon Sep 17 00:00:00 2001 From: 33 Date: Thu, 26 Jan 2023 19:34:58 +0800 Subject: [PATCH] Add: `GameProfile` cache for Mojang API --- .../customskinloader/loader/MojangAPILoader.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Common/source/customskinloader/loader/MojangAPILoader.java b/Common/source/customskinloader/loader/MojangAPILoader.java index 5d3379e..a8ef13e 100644 --- a/Common/source/customskinloader/loader/MojangAPILoader.java +++ b/Common/source/customskinloader/loader/MojangAPILoader.java @@ -5,6 +5,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; @@ -92,7 +93,7 @@ public class MojangAPILoader implements ICustomSkinLoaderPlugin, ProfileLoader.I return ModelManager0.toUserProfile(map); } String username = gameProfile.getName(); - GameProfile newGameProfile = loadGameProfile(ssp.apiRoot, username); + GameProfile newGameProfile = loadGameProfileCached(ssp.apiRoot, username); if (newGameProfile == null) { CustomSkinLoader.logger.info("Profile not found.(" + username + "'s profile not found.)"); return null; @@ -107,6 +108,13 @@ public class MojangAPILoader implements ICustomSkinLoaderPlugin, ProfileLoader.I return null; } + private static final Map gameProfileCache = new ConcurrentHashMap<>(); + + public static GameProfile loadGameProfileCached(String apiRoot, String username) { + return gameProfileCache.computeIfAbsent(apiRoot + " " + username, + ignored -> loadGameProfile(apiRoot, username)); + } + //Username -> UUID public static GameProfile loadGameProfile(String apiRoot, String username) { //Doc (https://wiki.vg/Mojang_API#Playernames_-.3E_UUIDs) @@ -140,7 +148,7 @@ public class MojangAPILoader implements ICustomSkinLoaderPlugin, ProfileLoader.I * @return UUID in Mojang API style string. Returns {@code null} if username not found in Mojang API. */ public static String getMojangUuidByUsername(String username, boolean standard) { - GameProfile profile = loadGameProfile(getMojangApiRoot(), username); + GameProfile profile = loadGameProfileCached(getMojangApiRoot(), username); if (profile == null) { return null; }