feat: cache uuids
This commit is contained in:
parent
da8c7e91ce
commit
37b1d5bd0c
6 changed files with 34 additions and 17 deletions
|
@ -26,7 +26,7 @@ public class CacheDetailedGUI {
|
||||||
private final Gui gui;
|
private final Gui gui;
|
||||||
|
|
||||||
public CacheDetailedGUI(Player player) {
|
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()
|
final List<String> loadedSkins = skins.entrySet().stream()
|
||||||
.filter(entry -> entry.getValue().isPresent())
|
.filter(entry -> entry.getValue().isPresent())
|
||||||
.map(Map.Entry::getKey)
|
.map(Map.Entry::getKey)
|
||||||
|
|
|
@ -14,7 +14,7 @@ public class CacheStatisticsItem extends SuppliedItem {
|
||||||
public CacheStatisticsItem() {
|
public CacheStatisticsItem() {
|
||||||
super(() -> {
|
super(() -> {
|
||||||
final ItemBuilder builder = new ItemBuilder(Material.BOOK);
|
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();
|
final CacheStats stats = cache.stats();
|
||||||
builder.setDisplayName("§fStatistics");
|
builder.setDisplayName("§fStatistics");
|
||||||
builder.addLoreLines(
|
builder.addLoreLines(
|
||||||
|
|
|
@ -17,9 +17,7 @@ public class InvalidateCacheItem extends SuppliedItem {
|
||||||
builder.addLoreLines(
|
builder.addLoreLines(
|
||||||
"§c§oNOT RECOMMENDED",
|
"§c§oNOT RECOMMENDED",
|
||||||
"§7Invalidates every skin entry present in the cache.",
|
"§7Invalidates every skin entry present in the cache.",
|
||||||
"§7Does not reset player disguises.",
|
"§7Does not reset player disguises.");
|
||||||
"§7Could be useful if a skin has been updated",
|
|
||||||
"§7recently and the cache is now outdated.");
|
|
||||||
return builder;
|
return builder;
|
||||||
}, (click) -> {
|
}, (click) -> {
|
||||||
final ClickType clickType = click.getClickType();
|
final ClickType clickType = click.getClickType();
|
||||||
|
@ -29,7 +27,7 @@ public class InvalidateCacheItem extends SuppliedItem {
|
||||||
final Player player = click.getPlayer();
|
final Player player = click.getPlayer();
|
||||||
final I18N i18n = new I18N(player);
|
final I18N i18n = new I18N(player);
|
||||||
player.sendMessage(i18n.translate(I18NDict.Event.Admin.Cache.INVALIDATE_ALL));
|
player.sendMessage(i18n.translate(I18NDict.Event.Admin.Cache.INVALIDATE_ALL));
|
||||||
NickoBukkit.getInstance().getMojangAPI().getCache().invalidateAll();
|
NickoBukkit.getInstance().getMojangAPI().getSkinCache().invalidateAll();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -11,7 +11,9 @@ public class InvalidateEntryItem extends SuppliedItem {
|
||||||
super(() -> {
|
super(() -> {
|
||||||
final ItemBuilder builder = new ItemBuilder(Material.PAPER);
|
final ItemBuilder builder = new ItemBuilder(Material.PAPER);
|
||||||
builder.setDisplayName("§fInvalidate specific entry");
|
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;
|
return builder;
|
||||||
}, (click) -> {
|
}, (click) -> {
|
||||||
final ClickType clickType = click.getClickType();
|
final ClickType clickType = click.getClickType();
|
||||||
|
|
|
@ -25,31 +25,47 @@ public class MojangAPI {
|
||||||
public static final String URL_SKIN = "https://sessionserver.mojang.com/session/minecraft/profile/{uuid}?unsigned=false";
|
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 Logger logger = Logger.getLogger("MojangAPI");
|
||||||
|
|
||||||
private final HashMap<String, String> uuidToName = new HashMap<>();
|
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
|
@Nonnull
|
||||||
public Optional<MojangSkin> load(@Nonnull String uuid) throws Exception {
|
public Optional<MojangSkin> load(@Nonnull String uuid) throws Exception {
|
||||||
return getSkinFromMojang(uuid);
|
return getSkinFromMojang(uuid);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private final LoadingCache<String, Optional<MojangSkin>> cache = CacheBuilder
|
private final LoadingCache<String, Optional<MojangSkin>> skinCache = CacheBuilder
|
||||||
.newBuilder()
|
.newBuilder()
|
||||||
.recordStats()
|
.recordStats()
|
||||||
.expireAfterWrite(24, TimeUnit.HOURS)
|
.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 {
|
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 {
|
public Optional<MojangSkin> getSkinWithoutCaching(String uuid) throws IOException {
|
||||||
return getSkinFromMojang(uuid);
|
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 String parametrizedUrl = URL_NAME.replace("{name}", name);
|
||||||
final JsonObject object = getRequestToUrl(parametrizedUrl);
|
final JsonObject object = getRequestToUrl(parametrizedUrl);
|
||||||
if (hasNoError(object)) {
|
if (hasNoError(object)) {
|
||||||
|
@ -62,7 +78,7 @@ public class MojangAPI {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void eraseFromCache(String uuid) {
|
public void eraseFromCache(String uuid) {
|
||||||
cache.invalidate(uuid);
|
skinCache.invalidate(uuid);
|
||||||
uuidToName.remove(uuid);
|
uuidToName.remove(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,7 +141,7 @@ public class MojangAPI {
|
||||||
return object.get("error") == null;
|
return object.get("error") == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LoadingCache<String, Optional<MojangSkin>> getCache() {
|
public LoadingCache<String, Optional<MojangSkin>> getSkinCache() {
|
||||||
return cache;
|
return skinCache;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@ import xyz.atnrch.nicko.storage.sql.SQLStorage;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
public class PlayerDataStore {
|
public class PlayerDataStore {
|
||||||
private final Storage storage;
|
private final Storage storage;
|
||||||
|
@ -57,7 +58,7 @@ public class PlayerDataStore {
|
||||||
return getData(uuid);
|
return getData(uuid);
|
||||||
}
|
}
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
} catch (IOException e) {
|
} catch (IOException | ExecutionException e) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue