diff --git a/build.gradle b/build.gradle index 42ed583..081ebf8 100644 --- a/build.gradle +++ b/build.gradle @@ -30,6 +30,7 @@ dependencies { testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.1.0' compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1' + compile group: 'com.github.ben-manes.caffeine', name: 'caffeine', version: '2.8.6' } test { diff --git a/src/main/java/eu/beezig/hiveapi/wrapper/HiveWrapper.java b/src/main/java/eu/beezig/hiveapi/wrapper/HiveWrapper.java index 1262f9c..2ecb203 100644 --- a/src/main/java/eu/beezig/hiveapi/wrapper/HiveWrapper.java +++ b/src/main/java/eu/beezig/hiveapi/wrapper/HiveWrapper.java @@ -1,5 +1,7 @@ package eu.beezig.hiveapi.wrapper; +import com.github.benmanes.caffeine.cache.AsyncCache; + import java.util.concurrent.ExecutorService; public class HiveWrapper { @@ -7,6 +9,7 @@ public class HiveWrapper { public static String USER_AGENT; public static String MAXTHAT_KEY; public static ExecutorService ASYNC_EXECUTOR; + public static AsyncCache usernameToUUIDCache; public static void setUserAgent(String userAgent) { USER_AGENT = userAgent; @@ -20,6 +23,10 @@ public static void setAsyncExecutor(ExecutorService executor) { ASYNC_EXECUTOR = executor; } + public static void setUUIDCache(AsyncCache usernameToUUIDCache) { + HiveWrapper.usernameToUUIDCache = usernameToUUIDCache; + } + public static void asyncExecute(Runnable callback) { if(ASYNC_EXECUTOR == null) throw new RuntimeException("Async executor wasn't properly set up. Run HiveWrapper#setAsyncExecutor first."); ASYNC_EXECUTOR.submit(callback); diff --git a/src/main/java/eu/beezig/hiveapi/wrapper/mojang/UsernameToUuid.java b/src/main/java/eu/beezig/hiveapi/wrapper/mojang/UsernameToUuid.java index 7e1b16e..e6daebc 100644 --- a/src/main/java/eu/beezig/hiveapi/wrapper/mojang/UsernameToUuid.java +++ b/src/main/java/eu/beezig/hiveapi/wrapper/mojang/UsernameToUuid.java @@ -1,15 +1,19 @@ package eu.beezig.hiveapi.wrapper.mojang; +import eu.beezig.hiveapi.wrapper.HiveWrapper; import eu.beezig.hiveapi.wrapper.utils.download.UrlBuilder; import eu.beezig.hiveapi.wrapper.utils.json.JObject; import java.util.concurrent.CompletableFuture; public class UsernameToUuid { - public static CompletableFuture getUUID(String username) { + if(HiveWrapper.usernameToUUIDCache == null) return loadUUID(username); + return HiveWrapper.usernameToUUIDCache.get(username, (k, exec) -> loadUUID(k)); + } + + private static CompletableFuture loadUUID(String username) { CompletableFuture responseFromMojang = JObject.get(new UrlBuilder().mojang(username).build()); return responseFromMojang.thenApplyAsync(json -> json.getString("id")); } - } diff --git a/src/test/java/eu/beezig/hiveapi/wrapper/test/Cache.java b/src/test/java/eu/beezig/hiveapi/wrapper/test/Cache.java new file mode 100644 index 0000000..7ad5671 --- /dev/null +++ b/src/test/java/eu/beezig/hiveapi/wrapper/test/Cache.java @@ -0,0 +1,23 @@ +package eu.beezig.hiveapi.wrapper.test; + +import com.github.benmanes.caffeine.cache.Caffeine; +import eu.beezig.hiveapi.wrapper.HiveWrapper; +import eu.beezig.hiveapi.wrapper.mojang.UsernameToUuid; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.Executors; + +public class Cache { + @BeforeAll + public static void setup() { + HiveWrapper.setAsyncExecutor(Executors.newSingleThreadExecutor()); + HiveWrapper.setUUIDCache(Caffeine.newBuilder().buildAsync()); + } + + @Test + public void correctProfile() { + assert "069a79f444e94726a5befca90e38aaf5".equals(UsernameToUuid.getUUID("Notch").join()); + assert HiveWrapper.usernameToUUIDCache.synchronous().getIfPresent("Notch") != null; + } +}