Fix servers not caching
All checks were successful
Deploy App / docker (ubuntu-latest, 2.44.0, 17, 3.8.5) (push) Successful in 1m38s

This commit is contained in:
Braydon 2024-04-10 07:24:24 -04:00
parent af9df6571c
commit c9426d11af

@ -358,20 +358,18 @@ public final class MojangService {
String lookupHostname = hostname; // The hostname used to lookup the server String lookupHostname = hostname; // The hostname used to lookup the server
int port = platform.getDefaultPort(); // Port to ping int port = platform.getDefaultPort(); // Port to ping
String portString = null;
if (hostname.contains(":")) { // Hostname contains a port if (hostname.contains(":")) { // Hostname contains a port
String[] split = hostname.split(":"); String[] split = hostname.split(":");
hostname = split[0]; hostname = split[0];
portString = split[1];
try { // Try and parse the port try { // Try and parse the port
port = Integer.parseInt(portString); port = Integer.parseInt(split[1]);
} catch (NumberFormatException ex) { // Invalid port } catch (NumberFormatException ex) { // Invalid port
throw new BadRequestException("Invalid port defined"); throw new BadRequestException("Invalid port defined");
} }
} }
// Check the cache for the server // Check the cache for the server
Optional<CachedMinecraftServer> cached = minecraftServerCache.findById(platform.name() + "-" + hostname + "-" + port); Optional<CachedMinecraftServer> cached = minecraftServerCache.findById(platform.name() + "-" + lookupHostname);
if (cached.isPresent()) { // Respond with the cache if present if (cached.isPresent()) { // Respond with the cache if present
log.info("Found server in cache: {}", hostname); log.info("Found server in cache: {}", hostname);
return cached.get(); return cached.get();
@ -384,7 +382,7 @@ public final class MojangService {
// Build our server model, cache it, and then return it // Build our server model, cache it, and then return it
CachedMinecraftServer minecraftServer = new CachedMinecraftServer( CachedMinecraftServer minecraftServer = new CachedMinecraftServer(
platform.name() + "-" + lookupHostname + "-" + (portString == null ? port : portString), platform.name() + "-" + lookupHostname,
platform.getPinger().ping(hostname, port), platform.getPinger().ping(hostname, port),
System.currentTimeMillis() System.currentTimeMillis()
); );