File tree Expand file tree Collapse file tree 4 files changed +37
-2
lines changed
main/java/eu/beezig/hiveapi/wrapper
test/java/eu/beezig/hiveapi/wrapper/test Expand file tree Collapse file tree 4 files changed +37
-2
lines changed Original file line number Diff line number Diff 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
3536test {
Original file line number Diff line number Diff line change 11package eu .beezig .hiveapi .wrapper ;
22
3+ import com .github .benmanes .caffeine .cache .AsyncCache ;
4+
35import java .util .concurrent .ExecutorService ;
46
57public 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 );
Original file line number Diff line number Diff line change 11package eu .beezig .hiveapi .wrapper .mojang ;
22
3+ import eu .beezig .hiveapi .wrapper .HiveWrapper ;
34import eu .beezig .hiveapi .wrapper .utils .download .UrlBuilder ;
45import eu .beezig .hiveapi .wrapper .utils .json .JObject ;
56
67import java .util .concurrent .CompletableFuture ;
78
89public 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments