world controller: fix caching, move to scheduled method

Guava memorize will block threads waiting for a new result
This commit is contained in:
Adam
2018-08-13 09:37:30 -04:00
parent f2791b9792
commit 26c6e252da

View File

@@ -24,15 +24,14 @@
*/ */
package net.runelite.http.service.worlds; package net.runelite.http.service.worlds;
import com.google.common.base.Suppliers;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.runelite.http.api.worlds.WorldResult; import net.runelite.http.api.worlds.WorldResult;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.CacheControl; import org.springframework.http.CacheControl;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@@ -44,24 +43,19 @@ public class WorldController
@Autowired @Autowired
private WorldsService worldsService; private WorldsService worldsService;
private final Supplier<WorldResult> worlds = Suppliers.memoizeWithExpiration(() -> private WorldResult worldResult;
{
try
{
return worldsService.getWorlds();
}
catch (IOException ex)
{
log.warn(null, ex);
throw new RuntimeException(ex);
}
}, 10, TimeUnit.MINUTES);
@RequestMapping @RequestMapping
public ResponseEntity<WorldResult> listWorlds() throws IOException public ResponseEntity<WorldResult> listWorlds() throws IOException
{ {
return ResponseEntity.ok() return ResponseEntity.ok()
.cacheControl(CacheControl.maxAge(10, TimeUnit.MINUTES).cachePublic()) .cacheControl(CacheControl.maxAge(10, TimeUnit.MINUTES).cachePublic())
.body(worldsService.getWorlds()); .body(worldResult);
}
@Scheduled(fixedDelay = 60_000L)
public void refreshWorlds() throws IOException
{
worldResult = worldsService.getWorlds();
} }
} }