http-service: add cache control to item price

This commit is contained in:
Adam
2018-01-10 15:47:54 -05:00
parent 0fda835422
commit 9021b9a600

View File

@@ -32,10 +32,10 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import net.runelite.http.api.RuneLiteAPI; import net.runelite.http.api.RuneLiteAPI;
@@ -50,6 +50,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.CacheControl;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
@@ -188,8 +190,7 @@ public class ItemService
} }
@RequestMapping("/{itemId}/price") @RequestMapping("/{itemId}/price")
public ItemPrice getPrice( public ResponseEntity<ItemPrice> itemPrice(
HttpServletResponse response,
@PathVariable int itemId, @PathVariable int itemId,
@RequestParam(required = false) Instant time @RequestParam(required = false) Instant time
) )
@@ -210,7 +211,7 @@ public class ItemService
if (item == null) if (item == null)
{ {
return null; return ResponseEntity.notFound().build();
} }
} }
@@ -221,7 +222,7 @@ public class ItemService
if (priceEntry == null) if (priceEntry == null)
{ {
// we maybe can't backfill this // we maybe can't backfill this
return null; return ResponseEntity.notFound().build();
} }
} }
else else
@@ -234,7 +235,7 @@ public class ItemService
if (prices == null || prices.isEmpty()) if (prices == null || prices.isEmpty())
{ {
return null; return ResponseEntity.notFound().build();
} }
// Get the most recent price // Get the most recent price
@@ -248,8 +249,10 @@ public class ItemService
itemPrice.setPrice(priceEntry.getPrice()); itemPrice.setPrice(priceEntry.getPrice());
itemPrice.setTime(priceEntry.getTime()); itemPrice.setTime(priceEntry.getTime());
response.setHeader(RUNELITE_CACHE, hit ? "HIT" : "MISS"); return ResponseEntity.ok()
return itemPrice; .header(RUNELITE_CACHE, hit ? "HIT" : "MISS")
.cacheControl(CacheControl.maxAge(30, TimeUnit.SECONDS))
.body(itemPrice);
} }
@RequestMapping("/search") @RequestMapping("/search")