From 40ff55558f75b54f979b72ba2624d0c2c8d51fe7 Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 9 Aug 2018 17:56:22 -0400 Subject: [PATCH] world service: split controller from service and add cache --- .../updatecheck/UpdateCheckService.java | 2 +- .../http/service/worlds/WorldController.java | 67 +++++++++++++++++++ .../http/service/worlds/WorldsService.java | 9 +-- .../service/worlds/WorldsServiceTest.java | 2 +- 4 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 http-service/src/main/java/net/runelite/http/service/worlds/WorldController.java diff --git a/http-service/src/main/java/net/runelite/http/service/updatecheck/UpdateCheckService.java b/http-service/src/main/java/net/runelite/http/service/updatecheck/UpdateCheckService.java index 4bbc968d5a..cfde4ad078 100644 --- a/http-service/src/main/java/net/runelite/http/service/updatecheck/UpdateCheckService.java +++ b/http-service/src/main/java/net/runelite/http/service/updatecheck/UpdateCheckService.java @@ -138,7 +138,7 @@ public class UpdateCheckService { try { - WorldResult worldResult = worldsService.listWorlds(); + WorldResult worldResult = worldsService.getWorlds(); List worlds = worldResult.getWorlds(); Random rand = new Random(); return worlds.get(rand.nextInt(worlds.size())); diff --git a/http-service/src/main/java/net/runelite/http/service/worlds/WorldController.java b/http-service/src/main/java/net/runelite/http/service/worlds/WorldController.java new file mode 100644 index 0000000000..893ee6278d --- /dev/null +++ b/http-service/src/main/java/net/runelite/http/service/worlds/WorldController.java @@ -0,0 +1,67 @@ +/* + * 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: + * + * 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 worlds = Suppliers.memoizeWithExpiration(() -> + { + try + { + return worldsService.getWorlds(); + } + catch (IOException ex) + { + log.warn(null, ex); + throw new RuntimeException(ex); + } + }, 10, TimeUnit.MINUTES); + + @RequestMapping + public ResponseEntity listWorlds() throws IOException + { + return ResponseEntity.ok() + .cacheControl(CacheControl.maxAge(10, TimeUnit.MINUTES).cachePublic()) + .body(worldsService.getWorlds()); + } +} diff --git a/http-service/src/main/java/net/runelite/http/service/worlds/WorldsService.java b/http-service/src/main/java/net/runelite/http/service/worlds/WorldsService.java index 85a22ebe9e..a3bd010430 100644 --- a/http-service/src/main/java/net/runelite/http/service/worlds/WorldsService.java +++ b/http-service/src/main/java/net/runelite/http/service/worlds/WorldsService.java @@ -36,19 +36,16 @@ import net.runelite.http.api.worlds.WorldType; import okhttp3.HttpUrl; import okhttp3.Request; import okhttp3.Response; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.stereotype.Service; -@RestController -@RequestMapping("/worlds") +@Service public class WorldsService { private static final HttpUrl WORLD_URL = HttpUrl.parse("http://www.runescape.com/g=oldscape/slr.ws?order=LPWM"); private HttpUrl url = WORLD_URL; - @RequestMapping - public WorldResult listWorlds() throws IOException + public WorldResult getWorlds() throws IOException { Request okrequest = new Request.Builder() .url(url) diff --git a/http-service/src/test/java/net/runelite/http/service/worlds/WorldsServiceTest.java b/http-service/src/test/java/net/runelite/http/service/worlds/WorldsServiceTest.java index d6cbc0f6fb..152a6e4e5a 100644 --- a/http-service/src/test/java/net/runelite/http/service/worlds/WorldsServiceTest.java +++ b/http-service/src/test/java/net/runelite/http/service/worlds/WorldsServiceTest.java @@ -71,7 +71,7 @@ public class WorldsServiceTest WorldsService worlds = new WorldsService(); worlds.setUrl(server.url("/")); - WorldResult worldResult = worlds.listWorlds(); + WorldResult worldResult = worlds.getWorlds(); assertEquals(82, worldResult.getWorlds().size()); World world = worldResult.findWorld(385);