http-service: queue item lookups from images
This commit is contained in:
@@ -85,43 +85,29 @@ public class ItemController
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/{itemId}/icon", produces = "image/gif")
|
||||
public byte[] getIcon(HttpServletResponse response, @PathVariable int itemId)
|
||||
public ResponseEntity<byte[]> getIcon(@PathVariable int itemId)
|
||||
{
|
||||
ItemEntry item = itemService.getItem(itemId);
|
||||
if (item != null && item.getIcon() != null)
|
||||
{
|
||||
response.setHeader(RUNELITE_CACHE, "HIT");
|
||||
return item.getIcon();
|
||||
return ResponseEntity.ok(item.getIcon());
|
||||
}
|
||||
|
||||
item = itemService.fetchItem(itemId);
|
||||
if (item != null)
|
||||
{
|
||||
response.setHeader(RUNELITE_CACHE, "MISS");
|
||||
return item.getIcon();
|
||||
}
|
||||
|
||||
return null;
|
||||
itemService.queueItem(itemId);
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
@RequestMapping(path = "/{itemId}/icon/large", produces = "image/gif")
|
||||
public byte[] getIconLarge(HttpServletResponse response, @PathVariable int itemId)
|
||||
public ResponseEntity<byte[]> getIconLarge(HttpServletResponse response, @PathVariable int itemId)
|
||||
{
|
||||
ItemEntry item = itemService.getItem(itemId);
|
||||
if (item != null && item.getIcon_large() != null)
|
||||
{
|
||||
response.setHeader(RUNELITE_CACHE, "HIT");
|
||||
return item.getIcon_large();
|
||||
return ResponseEntity.ok(item.getIcon_large());
|
||||
}
|
||||
|
||||
item = itemService.fetchItem(itemId);
|
||||
if (item != null)
|
||||
{
|
||||
response.setHeader(RUNELITE_CACHE, "MISS");
|
||||
return item.getIcon_large();
|
||||
}
|
||||
|
||||
return null;
|
||||
itemService.queueItem(itemId);
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
@RequestMapping("/{itemId}/price")
|
||||
@@ -195,7 +181,7 @@ public class ItemController
|
||||
if (priceEntry.getFetched_time().isBefore(cacheTime))
|
||||
{
|
||||
// Queue a check for the price
|
||||
itemService.queueLookup(itemId);
|
||||
itemService.queuePriceLookup(itemId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -86,6 +86,7 @@ public class ItemService
|
||||
private final Sql2o sql2o;
|
||||
private final ConcurrentLinkedQueue<Integer> pendingPriceLookups = new ConcurrentLinkedQueue<>();
|
||||
private final ConcurrentLinkedQueue<String> pendingSearches = new ConcurrentLinkedQueue<>();
|
||||
private final ConcurrentLinkedQueue<Integer> pendingItems = new ConcurrentLinkedQueue<>();
|
||||
|
||||
@Autowired
|
||||
public ItemService(@Qualifier("Runelite SQL2O") Sql2o sql2o)
|
||||
@@ -362,7 +363,7 @@ public class ItemService
|
||||
}
|
||||
}
|
||||
|
||||
public void queueLookup(int itemId)
|
||||
public void queuePriceLookup(int itemId)
|
||||
{
|
||||
if (pendingPriceLookups.size() >= MAX_PENDING)
|
||||
{
|
||||
@@ -382,25 +383,48 @@ public class ItemService
|
||||
pendingSearches.add(search);
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelay = 5000)
|
||||
public void checkPrices()
|
||||
public void queueItem(int itemId)
|
||||
{
|
||||
Integer itemId = pendingPriceLookups.poll();
|
||||
if (itemId == null)
|
||||
if (pendingItems.size() >= MAX_PENDING)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
fetchPrice(itemId);
|
||||
pendingItems.add(itemId);
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelay = 5000)
|
||||
public void checkSearches()
|
||||
public void check()
|
||||
{
|
||||
if (checkPrices())
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (checkSearches())
|
||||
{
|
||||
return;
|
||||
}
|
||||
checkItems();
|
||||
}
|
||||
|
||||
private boolean checkPrices()
|
||||
{
|
||||
Integer itemId = pendingPriceLookups.poll();
|
||||
if (itemId == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
fetchPrice(itemId);
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean checkSearches()
|
||||
{
|
||||
String search = pendingSearches.poll();
|
||||
if (search == null)
|
||||
{
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
@@ -413,5 +437,19 @@ public class ItemService
|
||||
{
|
||||
log.warn("error while searching items", ex);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean checkItems()
|
||||
{
|
||||
Integer itemId = pendingItems.poll();
|
||||
if (itemId == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
fetchItem(itemId);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user