Merge pull request #5429 from Adam-/hiscore-cache
http-service: add caching for hiscore lookups
This commit is contained in:
@@ -30,12 +30,12 @@ import okhttp3.HttpUrl;
|
|||||||
|
|
||||||
public enum HiscoreEndpoint
|
public enum HiscoreEndpoint
|
||||||
{
|
{
|
||||||
NORMAL("Normal", "http://services.runescape.com/m=hiscore_oldschool/index_lite.ws"),
|
NORMAL("Normal", "https://services.runescape.com/m=hiscore_oldschool/index_lite.ws"),
|
||||||
IRONMAN("Ironman", "http://services.runescape.com/m=hiscore_oldschool_ironman/index_lite.ws"),
|
IRONMAN("Ironman", "https://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"),
|
HARDCORE_IRONMAN("Hardcore Ironman", "https://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"),
|
ULTIMATE_IRONMAN("Ultimate Ironman", "https://services.runescape.com/m=hiscore_oldschool_ultimate/index_lite.ws"),
|
||||||
DEADMAN("Deadman", "http://services.runescape.com/m=hiscore_oldschool_deadman/index_lite.ws"),
|
DEADMAN("Deadman", "https://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");
|
SEASONAL_DEADMAN("Seasonal Deadman", "https://services.runescape.com/m=hiscore_oldschool_seasonal/index_lite.ws");
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
private final HttpUrl hiscoreURL;
|
private final HttpUrl hiscoreURL;
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
*/
|
*/
|
||||||
package net.runelite.http.service.hiscore;
|
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.HiscoreEndpoint;
|
||||||
import net.runelite.http.api.hiscore.HiscoreResult;
|
import net.runelite.http.api.hiscore.HiscoreResult;
|
||||||
import net.runelite.http.api.hiscore.HiscoreSkill;
|
import net.runelite.http.api.hiscore.HiscoreSkill;
|
||||||
@@ -51,7 +51,7 @@ public class HiscoreController
|
|||||||
private XpTrackerService xpTrackerService;
|
private XpTrackerService xpTrackerService;
|
||||||
|
|
||||||
@RequestMapping("/{endpoint}")
|
@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);
|
HiscoreResultBuilder resultBuilder = hiscoreService.lookupUsername(username, endpoint);
|
||||||
HiscoreResult result = resultBuilder.build();
|
HiscoreResult result = resultBuilder.build();
|
||||||
@@ -70,7 +70,7 @@ public class HiscoreController
|
|||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("/{endpoint}/{skillName}")
|
@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());
|
HiscoreSkill skill = HiscoreSkill.valueOf(skillName.toUpperCase());
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018, Adam <Adam@sigterm.info>
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
@@ -24,7 +24,12 @@
|
|||||||
*/
|
*/
|
||||||
package net.runelite.http.service.hiscore;
|
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.io.IOException;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.runelite.http.api.RuneLiteAPI;
|
import net.runelite.http.api.RuneLiteAPI;
|
||||||
import net.runelite.http.api.hiscore.HiscoreEndpoint;
|
import net.runelite.http.api.hiscore.HiscoreEndpoint;
|
||||||
@@ -45,12 +50,25 @@ import org.springframework.stereotype.Service;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class HiscoreService
|
public class HiscoreService
|
||||||
{
|
{
|
||||||
public HiscoreResultBuilder lookupUsername(String username, HiscoreEndpoint endpoint) throws IOException
|
private final LoadingCache<HiscoreKey, HiscoreResultBuilder> hiscoreCache = CacheBuilder.newBuilder()
|
||||||
|
.maximumSize(128)
|
||||||
|
.expireAfterWrite(1, TimeUnit.MINUTES)
|
||||||
|
.build(
|
||||||
|
new CacheLoader<HiscoreKey, HiscoreResultBuilder>()
|
||||||
|
{
|
||||||
|
@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()
|
HttpUrl url = hiscoreUrl.newBuilder()
|
||||||
.addQueryParameter("player", username)
|
.addQueryParameter("player", username)
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ package net.runelite.http.service.xp;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
import net.runelite.http.api.xp.XpData;
|
import net.runelite.http.api.xp.XpData;
|
||||||
import net.runelite.http.service.xp.beans.XpEntity;
|
import net.runelite.http.service.xp.beans.XpEntity;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -41,7 +42,7 @@ public class XpTrackerController
|
|||||||
private XpTrackerService xpTrackerService;
|
private XpTrackerService xpTrackerService;
|
||||||
|
|
||||||
@RequestMapping("/update")
|
@RequestMapping("/update")
|
||||||
public void update(@RequestParam String username) throws IOException
|
public void update(@RequestParam String username) throws ExecutionException
|
||||||
{
|
{
|
||||||
xpTrackerService.update(username);
|
xpTrackerService.update(username);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,8 +24,8 @@
|
|||||||
*/
|
*/
|
||||||
package net.runelite.http.service.xp;
|
package net.runelite.http.service.xp;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.runelite.http.api.hiscore.HiscoreEndpoint;
|
import net.runelite.http.api.hiscore.HiscoreEndpoint;
|
||||||
import net.runelite.http.api.hiscore.HiscoreResult;
|
import net.runelite.http.api.hiscore.HiscoreResult;
|
||||||
@@ -51,7 +51,7 @@ public class XpTrackerService
|
|||||||
@Autowired
|
@Autowired
|
||||||
private HiscoreService hiscoreService;
|
private HiscoreService hiscoreService;
|
||||||
|
|
||||||
public void update(String username) throws IOException
|
public void update(String username) throws ExecutionException
|
||||||
{
|
{
|
||||||
HiscoreResultBuilder hiscoreResultBuilder = hiscoreService.lookupUsername(username, HiscoreEndpoint.NORMAL);
|
HiscoreResultBuilder hiscoreResultBuilder = hiscoreService.lookupUsername(username, HiscoreEndpoint.NORMAL);
|
||||||
HiscoreResult hiscoreResult = hiscoreResultBuilder.build();
|
HiscoreResult hiscoreResult = hiscoreResultBuilder.build();
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
package net.runelite.http.service.hiscore;
|
package net.runelite.http.service.hiscore;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
import net.runelite.http.api.hiscore.HiscoreEndpoint;
|
import net.runelite.http.api.hiscore.HiscoreEndpoint;
|
||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
|
|
||||||
@@ -39,8 +40,15 @@ class HiscoreTestService extends HiscoreService
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user