feat: cache uuids

This commit is contained in:
ineanto 2023-07-08 16:09:37 +02:00
parent da8c7e91ce
commit 37b1d5bd0c
6 changed files with 34 additions and 17 deletions

View file

@ -26,7 +26,7 @@ public class CacheDetailedGUI {
private final Gui gui;
public CacheDetailedGUI(Player player) {
final ConcurrentMap<String, Optional<MojangSkin>> skins = NickoBukkit.getInstance().getMojangAPI().getCache().asMap();
final ConcurrentMap<String, Optional<MojangSkin>> skins = NickoBukkit.getInstance().getMojangAPI().getSkinCache().asMap();
final List<String> loadedSkins = skins.entrySet().stream()
.filter(entry -> entry.getValue().isPresent())
.map(Map.Entry::getKey)

View file

@ -14,7 +14,7 @@ public class CacheStatisticsItem extends SuppliedItem {
public CacheStatisticsItem() {
super(() -> {
final ItemBuilder builder = new ItemBuilder(Material.BOOK);
final LoadingCache<String, Optional<MojangSkin>> cache = NickoBukkit.getInstance().getMojangAPI().getCache();
final LoadingCache<String, Optional<MojangSkin>> cache = NickoBukkit.getInstance().getMojangAPI().getSkinCache();
final CacheStats stats = cache.stats();
builder.setDisplayName("§fStatistics");
builder.addLoreLines(

View file

@ -17,9 +17,7 @@ public class InvalidateCacheItem extends SuppliedItem {
builder.addLoreLines(
"§c§oNOT RECOMMENDED",
"§7Invalidates every skin entry present in the cache.",
"§7Does not reset player disguises.",
"§7Could be useful if a skin has been updated",
"§7recently and the cache is now outdated.");
"§7Does not reset player disguises.");
return builder;
}, (click) -> {
final ClickType clickType = click.getClickType();
@ -29,7 +27,7 @@ public class InvalidateCacheItem extends SuppliedItem {
final Player player = click.getPlayer();
final I18N i18n = new I18N(player);
player.sendMessage(i18n.translate(I18NDict.Event.Admin.Cache.INVALIDATE_ALL));
NickoBukkit.getInstance().getMojangAPI().getCache().invalidateAll();
NickoBukkit.getInstance().getMojangAPI().getSkinCache().invalidateAll();
return true;
}
return false;

View file

@ -11,7 +11,9 @@ public class InvalidateEntryItem extends SuppliedItem {
super(() -> {
final ItemBuilder builder = new ItemBuilder(Material.PAPER);
builder.setDisplayName("§fInvalidate specific entry");
builder.addLoreLines("§7Select a specific skin to invalidate.");
builder.addLoreLines("§7Select a specific skin to invalidate.",
"§7Useful if a skin has been updated",
"§7recently and the cache no longer up-to-date.");
return builder;
}, (click) -> {
final ClickType clickType = click.getClickType();

View file

@ -25,31 +25,47 @@ public class MojangAPI {
public static final String URL_SKIN = "https://sessionserver.mojang.com/session/minecraft/profile/{uuid}?unsigned=false";
private final Logger logger = Logger.getLogger("MojangAPI");
private final HashMap<String, String> uuidToName = new HashMap<>();
private final CacheLoader<String, Optional<MojangSkin>> loader = new CacheLoader<String, Optional<MojangSkin>>() {
private final CacheLoader<String, Optional<MojangSkin>> skinLoader = new CacheLoader<String, Optional<MojangSkin>>() {
@Nonnull
public Optional<MojangSkin> load(@Nonnull String uuid) throws Exception {
return getSkinFromMojang(uuid);
}
};
private final LoadingCache<String, Optional<MojangSkin>> cache = CacheBuilder
private final LoadingCache<String, Optional<MojangSkin>> skinCache = CacheBuilder
.newBuilder()
.recordStats()
.expireAfterWrite(24, TimeUnit.HOURS)
.build(loader);
.build(skinLoader);
private final CacheLoader<String, Optional<String>> uuidLoader = new CacheLoader<String, Optional<String>>() {
@Nonnull
public Optional<String> load(@Nonnull String name) throws Exception {
return getUUIDFromMojang(name);
}
};
private final LoadingCache<String, Optional<String>> uuidCache = CacheBuilder
.newBuilder()
.expireAfterWrite(2, TimeUnit.DAYS)
.build(uuidLoader);
public Optional<MojangSkin> getSkin(String uuid) throws IOException, ExecutionException {
return cache.get(uuid);
return skinCache.get(uuid);
}
public Optional<MojangSkin> getSkinWithoutCaching(String uuid) throws IOException {
return getSkinFromMojang(uuid);
}
public Optional<String> getUUID(String name) throws IOException {
public Optional<String> getUUID(String name) throws IOException, ExecutionException {
return uuidCache.get(name);
}
private Optional<String> getUUIDFromMojang(String name) throws IOException {
// TODO (Ineanto, 7/6/23): store uuid
final String parametrizedUrl = URL_NAME.replace("{name}", name);
final JsonObject object = getRequestToUrl(parametrizedUrl);
if (hasNoError(object)) {
@ -62,7 +78,7 @@ public class MojangAPI {
}
public void eraseFromCache(String uuid) {
cache.invalidate(uuid);
skinCache.invalidate(uuid);
uuidToName.remove(uuid);
}
@ -125,7 +141,7 @@ public class MojangAPI {
return object.get("error") == null;
}
public LoadingCache<String, Optional<MojangSkin>> getCache() {
return cache;
public LoadingCache<String, Optional<MojangSkin>> getSkinCache() {
return skinCache;
}
}

View file

@ -15,6 +15,7 @@ import xyz.atnrch.nicko.storage.sql.SQLStorage;
import java.io.IOException;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
public class PlayerDataStore {
private final Storage storage;
@ -57,7 +58,7 @@ public class PlayerDataStore {
return getData(uuid);
}
return Optional.empty();
} catch (IOException e) {
} catch (IOException | ExecutionException e) {
return Optional.empty();
}
}