diff --git a/http-api/src/main/java/net/runelite/http/api/hiscore/HiscoreEndpoint.java b/http-api/src/main/java/net/runelite/http/api/hiscore/HiscoreEndpoint.java index c9e11529a1..adfb017372 100644 --- a/http-api/src/main/java/net/runelite/http/api/hiscore/HiscoreEndpoint.java +++ b/http-api/src/main/java/net/runelite/http/api/hiscore/HiscoreEndpoint.java @@ -30,12 +30,12 @@ import okhttp3.HttpUrl; public enum HiscoreEndpoint { - NORMAL("Normal", "http://services.runescape.com/m=hiscore_oldschool/index_lite.ws"), - IRONMAN("Ironman", "http://services.runescape.com/m=hiscore_oldschool_ironman/index_lite.ws"), - HARDCORE_IRONMAN("Hardcore Ironman", "http://services.runescape.com/m=hiscore_oldschool_hardcore_ironman/index_lite.ws"), - ULTIMATE_IRONMAN("Ultimate Ironman", "http://services.runescape.com/m=hiscore_oldschool_ultimate/index_lite.ws"), - DEADMAN("Deadman", "http://services.runescape.com/m=hiscore_oldschool_deadman/index_lite.ws"), - SEASONAL_DEADMAN("Seasonal Deadman", "http://services.runescape.com/m=hiscore_oldschool_seasonal/index_lite.ws"); + NORMAL("Normal", "https://services.runescape.com/m=hiscore_oldschool/index_lite.ws"), + IRONMAN("Ironman", "https://services.runescape.com/m=hiscore_oldschool_ironman/index_lite.ws"), + HARDCORE_IRONMAN("Hardcore Ironman", "https://services.runescape.com/m=hiscore_oldschool_hardcore_ironman/index_lite.ws"), + ULTIMATE_IRONMAN("Ultimate Ironman", "https://services.runescape.com/m=hiscore_oldschool_ultimate/index_lite.ws"), + DEADMAN("Deadman", "https://services.runescape.com/m=hiscore_oldschool_deadman/index_lite.ws"), + SEASONAL_DEADMAN("Seasonal Deadman", "https://services.runescape.com/m=hiscore_oldschool_seasonal/index_lite.ws"); private final String name; private final HttpUrl hiscoreURL; diff --git a/http-service/src/main/java/net/runelite/http/service/hiscore/HiscoreController.java b/http-service/src/main/java/net/runelite/http/service/hiscore/HiscoreController.java index 9b581dcd87..203c72daeb 100644 --- a/http-service/src/main/java/net/runelite/http/service/hiscore/HiscoreController.java +++ b/http-service/src/main/java/net/runelite/http/service/hiscore/HiscoreController.java @@ -24,7 +24,7 @@ */ package net.runelite.http.service.hiscore; -import java.io.IOException; +import java.util.concurrent.ExecutionException; import net.runelite.http.api.hiscore.HiscoreEndpoint; import net.runelite.http.api.hiscore.HiscoreResult; import net.runelite.http.api.hiscore.HiscoreSkill; @@ -51,7 +51,7 @@ public class HiscoreController private XpTrackerService xpTrackerService; @RequestMapping("/{endpoint}") - public HiscoreResult lookup(@PathVariable HiscoreEndpoint endpoint, @RequestParam String username) throws IOException + public HiscoreResult lookup(@PathVariable HiscoreEndpoint endpoint, @RequestParam String username) throws ExecutionException { HiscoreResultBuilder resultBuilder = hiscoreService.lookupUsername(username, endpoint); HiscoreResult result = resultBuilder.build(); @@ -70,7 +70,7 @@ public class HiscoreController } @RequestMapping("/{endpoint}/{skillName}") - public SingleHiscoreSkillResult singleSkillLookup(@PathVariable HiscoreEndpoint endpoint, @PathVariable String skillName, @RequestParam String username) throws IOException + public SingleHiscoreSkillResult singleSkillLookup(@PathVariable HiscoreEndpoint endpoint, @PathVariable String skillName, @RequestParam String username) throws ExecutionException { HiscoreSkill skill = HiscoreSkill.valueOf(skillName.toUpperCase()); diff --git a/http-service/src/main/java/net/runelite/http/service/hiscore/HiscoreKey.java b/http-service/src/main/java/net/runelite/http/service/hiscore/HiscoreKey.java new file mode 100644 index 0000000000..b15603a224 --- /dev/null +++ b/http-service/src/main/java/net/runelite/http/service/hiscore/HiscoreKey.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2018, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.http.service.hiscore; + +import lombok.Value; +import net.runelite.http.api.hiscore.HiscoreEndpoint; + +@Value +class HiscoreKey +{ + String username; + HiscoreEndpoint endpoint; +} diff --git a/http-service/src/main/java/net/runelite/http/service/hiscore/HiscoreService.java b/http-service/src/main/java/net/runelite/http/service/hiscore/HiscoreService.java index e52c35d986..8487e1ad38 100644 --- a/http-service/src/main/java/net/runelite/http/service/hiscore/HiscoreService.java +++ b/http-service/src/main/java/net/runelite/http/service/hiscore/HiscoreService.java @@ -24,7 +24,12 @@ */ package net.runelite.http.service.hiscore; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; import lombok.extern.slf4j.Slf4j; import net.runelite.http.api.RuneLiteAPI; import net.runelite.http.api.hiscore.HiscoreEndpoint; @@ -45,12 +50,25 @@ import org.springframework.stereotype.Service; @Slf4j public class HiscoreService { - public HiscoreResultBuilder lookupUsername(String username, HiscoreEndpoint endpoint) throws IOException + private final LoadingCache hiscoreCache = CacheBuilder.newBuilder() + .maximumSize(128) + .expireAfterWrite(1, TimeUnit.MINUTES) + .build( + new CacheLoader() + { + @Override + public HiscoreResultBuilder load(HiscoreKey key) throws IOException + { + return lookupUsername(key.getUsername(), key.getEndpoint().getHiscoreURL()); + } + }); + + public HiscoreResultBuilder lookupUsername(String username, HiscoreEndpoint endpoint) throws ExecutionException { - return lookupUsername(username, endpoint.getHiscoreURL()); + return hiscoreCache.get(new HiscoreKey(username, endpoint)); } - public HiscoreResultBuilder lookupUsername(String username, HttpUrl hiscoreUrl) throws IOException + HiscoreResultBuilder lookupUsername(String username, HttpUrl hiscoreUrl) throws IOException { HttpUrl url = hiscoreUrl.newBuilder() .addQueryParameter("player", username) diff --git a/http-service/src/main/java/net/runelite/http/service/xp/XpTrackerController.java b/http-service/src/main/java/net/runelite/http/service/xp/XpTrackerController.java index 7606d4e4ee..0dc3aff491 100644 --- a/http-service/src/main/java/net/runelite/http/service/xp/XpTrackerController.java +++ b/http-service/src/main/java/net/runelite/http/service/xp/XpTrackerController.java @@ -26,6 +26,7 @@ package net.runelite.http.service.xp; import java.io.IOException; import java.time.Instant; +import java.util.concurrent.ExecutionException; import net.runelite.http.api.xp.XpData; import net.runelite.http.service.xp.beans.XpEntity; import org.springframework.beans.factory.annotation.Autowired; @@ -41,7 +42,7 @@ public class XpTrackerController private XpTrackerService xpTrackerService; @RequestMapping("/update") - public void update(@RequestParam String username) throws IOException + public void update(@RequestParam String username) throws ExecutionException { xpTrackerService.update(username); } diff --git a/http-service/src/main/java/net/runelite/http/service/xp/XpTrackerService.java b/http-service/src/main/java/net/runelite/http/service/xp/XpTrackerService.java index 63c8d7580c..7e6fe4271a 100644 --- a/http-service/src/main/java/net/runelite/http/service/xp/XpTrackerService.java +++ b/http-service/src/main/java/net/runelite/http/service/xp/XpTrackerService.java @@ -24,8 +24,8 @@ */ package net.runelite.http.service.xp; -import java.io.IOException; import java.time.Instant; +import java.util.concurrent.ExecutionException; import lombok.extern.slf4j.Slf4j; import net.runelite.http.api.hiscore.HiscoreEndpoint; import net.runelite.http.api.hiscore.HiscoreResult; @@ -51,7 +51,7 @@ public class XpTrackerService @Autowired private HiscoreService hiscoreService; - public void update(String username) throws IOException + public void update(String username) throws ExecutionException { HiscoreResultBuilder hiscoreResultBuilder = hiscoreService.lookupUsername(username, HiscoreEndpoint.NORMAL); HiscoreResult hiscoreResult = hiscoreResultBuilder.build(); diff --git a/http-service/src/test/java/net/runelite/http/service/hiscore/HiscoreTestService.java b/http-service/src/test/java/net/runelite/http/service/hiscore/HiscoreTestService.java index 4290cd1d1a..c5a35964d0 100644 --- a/http-service/src/test/java/net/runelite/http/service/hiscore/HiscoreTestService.java +++ b/http-service/src/test/java/net/runelite/http/service/hiscore/HiscoreTestService.java @@ -26,6 +26,7 @@ package net.runelite.http.service.hiscore; import java.io.IOException; +import java.util.concurrent.ExecutionException; import net.runelite.http.api.hiscore.HiscoreEndpoint; import okhttp3.HttpUrl; @@ -39,8 +40,15 @@ class HiscoreTestService extends HiscoreService } @Override - public HiscoreResultBuilder lookupUsername(String username, HiscoreEndpoint endpoint) throws IOException + public HiscoreResultBuilder lookupUsername(String username, HiscoreEndpoint endpoint) throws ExecutionException { - return super.lookupUsername(username, testUrl); + try + { + return super.lookupUsername(username, testUrl); + } + catch (IOException e) + { + throw new ExecutionException(e); + } } }