Skip to content

Commit

Permalink
uuid: add cache support
Browse files Browse the repository at this point in the history
  • Loading branch information
roccodev committed Oct 22, 2020
1 parent 7d050ec commit fff7049
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/eu/beezig/hiveapi/wrapper/HiveWrapper.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package eu.beezig.hiveapi.wrapper;

import com.github.benmanes.caffeine.cache.AsyncCache;

import java.util.concurrent.ExecutorService;

public class HiveWrapper {

public static String USER_AGENT;
public static String MAXTHAT_KEY;
public static ExecutorService ASYNC_EXECUTOR;
public static AsyncCache<String, String> usernameToUUIDCache;

public static void setUserAgent(String userAgent) {
USER_AGENT = userAgent;
Expand All @@ -20,6 +23,10 @@ public static void setAsyncExecutor(ExecutorService executor) {
ASYNC_EXECUTOR = executor;
}

public static void setUUIDCache(AsyncCache<String, String> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> getUUID(String username) {
if(HiveWrapper.usernameToUUIDCache == null) return loadUUID(username);
return HiveWrapper.usernameToUUIDCache.get(username, (k, exec) -> loadUUID(k));
}

private static CompletableFuture<String> loadUUID(String username) {
CompletableFuture<JObject> responseFromMojang = JObject.get(new UrlBuilder().mojang(username).build());
return responseFromMojang.thenApplyAsync(json -> json.getString("id"));
}

}
23 changes: 23 additions & 0 deletions src/test/java/eu/beezig/hiveapi/wrapper/test/Cache.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit fff7049

Please sign in to comment.