Skip to content

Commit fff7049

Browse files
committed
uuid: add cache support
1 parent 7d050ec commit fff7049

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies {
3030
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.1.0'
3131

3232
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'
33+
compile group: 'com.github.ben-manes.caffeine', name: 'caffeine', version: '2.8.6'
3334
}
3435

3536
test {

src/main/java/eu/beezig/hiveapi/wrapper/HiveWrapper.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package eu.beezig.hiveapi.wrapper;
22

3+
import com.github.benmanes.caffeine.cache.AsyncCache;
4+
35
import java.util.concurrent.ExecutorService;
46

57
public class HiveWrapper {
68

79
public static String USER_AGENT;
810
public static String MAXTHAT_KEY;
911
public static ExecutorService ASYNC_EXECUTOR;
12+
public static AsyncCache<String, String> usernameToUUIDCache;
1013

1114
public static void setUserAgent(String userAgent) {
1215
USER_AGENT = userAgent;
@@ -20,6 +23,10 @@ public static void setAsyncExecutor(ExecutorService executor) {
2023
ASYNC_EXECUTOR = executor;
2124
}
2225

26+
public static void setUUIDCache(AsyncCache<String, String> usernameToUUIDCache) {
27+
HiveWrapper.usernameToUUIDCache = usernameToUUIDCache;
28+
}
29+
2330
public static void asyncExecute(Runnable callback) {
2431
if(ASYNC_EXECUTOR == null) throw new RuntimeException("Async executor wasn't properly set up. Run HiveWrapper#setAsyncExecutor first.");
2532
ASYNC_EXECUTOR.submit(callback);
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package eu.beezig.hiveapi.wrapper.mojang;
22

3+
import eu.beezig.hiveapi.wrapper.HiveWrapper;
34
import eu.beezig.hiveapi.wrapper.utils.download.UrlBuilder;
45
import eu.beezig.hiveapi.wrapper.utils.json.JObject;
56

67
import java.util.concurrent.CompletableFuture;
78

89
public class UsernameToUuid {
9-
1010
public static CompletableFuture<String> getUUID(String username) {
11+
if(HiveWrapper.usernameToUUIDCache == null) return loadUUID(username);
12+
return HiveWrapper.usernameToUUIDCache.get(username, (k, exec) -> loadUUID(k));
13+
}
14+
15+
private static CompletableFuture<String> loadUUID(String username) {
1116
CompletableFuture<JObject> responseFromMojang = JObject.get(new UrlBuilder().mojang(username).build());
1217
return responseFromMojang.thenApplyAsync(json -> json.getString("id"));
1318
}
14-
1519
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package eu.beezig.hiveapi.wrapper.test;
2+
3+
import com.github.benmanes.caffeine.cache.Caffeine;
4+
import eu.beezig.hiveapi.wrapper.HiveWrapper;
5+
import eu.beezig.hiveapi.wrapper.mojang.UsernameToUuid;
6+
import org.junit.jupiter.api.BeforeAll;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.util.concurrent.Executors;
10+
11+
public class Cache {
12+
@BeforeAll
13+
public static void setup() {
14+
HiveWrapper.setAsyncExecutor(Executors.newSingleThreadExecutor());
15+
HiveWrapper.setUUIDCache(Caffeine.newBuilder().buildAsync());
16+
}
17+
18+
@Test
19+
public void correctProfile() {
20+
assert "069a79f444e94726a5befca90e38aaf5".equals(UsernameToUuid.getUUID("Notch").join());
21+
assert HiveWrapper.usernameToUUIDCache.synchronous().getIfPresent("Notch") != null;
22+
}
23+
}

0 commit comments

Comments
 (0)