world service: split controller from service and add cache
This commit is contained in:
@@ -138,7 +138,7 @@ public class UpdateCheckService
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
WorldResult worldResult = worldsService.listWorlds();
|
WorldResult worldResult = worldsService.getWorlds();
|
||||||
List<World> worlds = worldResult.getWorlds();
|
List<World> worlds = worldResult.getWorlds();
|
||||||
Random rand = new Random();
|
Random rand = new Random();
|
||||||
return worlds.get(rand.nextInt(worlds.size()));
|
return worlds.get(rand.nextInt(worlds.size()));
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* 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:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
* 2. 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 OWNER 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.worlds;
|
||||||
|
|
||||||
|
import com.google.common.base.Suppliers;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import net.runelite.http.api.worlds.WorldResult;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.CacheControl;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/worlds")
|
||||||
|
@Slf4j
|
||||||
|
public class WorldController
|
||||||
|
{
|
||||||
|
@Autowired
|
||||||
|
private WorldsService worldsService;
|
||||||
|
|
||||||
|
private final Supplier<WorldResult> worlds = Suppliers.memoizeWithExpiration(() ->
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return worldsService.getWorlds();
|
||||||
|
}
|
||||||
|
catch (IOException ex)
|
||||||
|
{
|
||||||
|
log.warn(null, ex);
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
}, 10, TimeUnit.MINUTES);
|
||||||
|
|
||||||
|
@RequestMapping
|
||||||
|
public ResponseEntity<WorldResult> listWorlds() throws IOException
|
||||||
|
{
|
||||||
|
return ResponseEntity.ok()
|
||||||
|
.cacheControl(CacheControl.maxAge(10, TimeUnit.MINUTES).cachePublic())
|
||||||
|
.body(worldsService.getWorlds());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -36,19 +36,16 @@ import net.runelite.http.api.worlds.WorldType;
|
|||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@RestController
|
@Service
|
||||||
@RequestMapping("/worlds")
|
|
||||||
public class WorldsService
|
public class WorldsService
|
||||||
{
|
{
|
||||||
private static final HttpUrl WORLD_URL = HttpUrl.parse("http://www.runescape.com/g=oldscape/slr.ws?order=LPWM");
|
private static final HttpUrl WORLD_URL = HttpUrl.parse("http://www.runescape.com/g=oldscape/slr.ws?order=LPWM");
|
||||||
|
|
||||||
private HttpUrl url = WORLD_URL;
|
private HttpUrl url = WORLD_URL;
|
||||||
|
|
||||||
@RequestMapping
|
public WorldResult getWorlds() throws IOException
|
||||||
public WorldResult listWorlds() throws IOException
|
|
||||||
{
|
{
|
||||||
Request okrequest = new Request.Builder()
|
Request okrequest = new Request.Builder()
|
||||||
.url(url)
|
.url(url)
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ public class WorldsServiceTest
|
|||||||
WorldsService worlds = new WorldsService();
|
WorldsService worlds = new WorldsService();
|
||||||
worlds.setUrl(server.url("/"));
|
worlds.setUrl(server.url("/"));
|
||||||
|
|
||||||
WorldResult worldResult = worlds.listWorlds();
|
WorldResult worldResult = worlds.getWorlds();
|
||||||
assertEquals(82, worldResult.getWorlds().size());
|
assertEquals(82, worldResult.getWorlds().size());
|
||||||
|
|
||||||
World world = worldResult.findWorld(385);
|
World world = worldResult.findWorld(385);
|
||||||
|
|||||||
Reference in New Issue
Block a user