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")
|
@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);
|
ItemEntry item = itemService.getItem(itemId);
|
||||||
if (item != null && item.getIcon() != null)
|
if (item != null && item.getIcon() != null)
|
||||||
{
|
{
|
||||||
response.setHeader(RUNELITE_CACHE, "HIT");
|
return ResponseEntity.ok(item.getIcon());
|
||||||
return item.getIcon();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
item = itemService.fetchItem(itemId);
|
itemService.queueItem(itemId);
|
||||||
if (item != null)
|
return ResponseEntity.notFound().build();
|
||||||
{
|
|
||||||
response.setHeader(RUNELITE_CACHE, "MISS");
|
|
||||||
return item.getIcon();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(path = "/{itemId}/icon/large", produces = "image/gif")
|
@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);
|
ItemEntry item = itemService.getItem(itemId);
|
||||||
if (item != null && item.getIcon_large() != null)
|
if (item != null && item.getIcon_large() != null)
|
||||||
{
|
{
|
||||||
response.setHeader(RUNELITE_CACHE, "HIT");
|
return ResponseEntity.ok(item.getIcon_large());
|
||||||
return item.getIcon_large();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
item = itemService.fetchItem(itemId);
|
itemService.queueItem(itemId);
|
||||||
if (item != null)
|
return ResponseEntity.notFound().build();
|
||||||
{
|
|
||||||
response.setHeader(RUNELITE_CACHE, "MISS");
|
|
||||||
return item.getIcon_large();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("/{itemId}/price")
|
@RequestMapping("/{itemId}/price")
|
||||||
@@ -195,7 +181,7 @@ public class ItemController
|
|||||||
if (priceEntry.getFetched_time().isBefore(cacheTime))
|
if (priceEntry.getFetched_time().isBefore(cacheTime))
|
||||||
{
|
{
|
||||||
// Queue a check for the price
|
// 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 Sql2o sql2o;
|
||||||
private final ConcurrentLinkedQueue<Integer> pendingPriceLookups = new ConcurrentLinkedQueue<>();
|
private final ConcurrentLinkedQueue<Integer> pendingPriceLookups = new ConcurrentLinkedQueue<>();
|
||||||
private final ConcurrentLinkedQueue<String> pendingSearches = new ConcurrentLinkedQueue<>();
|
private final ConcurrentLinkedQueue<String> pendingSearches = new ConcurrentLinkedQueue<>();
|
||||||
|
private final ConcurrentLinkedQueue<Integer> pendingItems = new ConcurrentLinkedQueue<>();
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public ItemService(@Qualifier("Runelite SQL2O") Sql2o sql2o)
|
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)
|
if (pendingPriceLookups.size() >= MAX_PENDING)
|
||||||
{
|
{
|
||||||
@@ -382,25 +383,48 @@ public class ItemService
|
|||||||
pendingSearches.add(search);
|
pendingSearches.add(search);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Scheduled(fixedDelay = 5000)
|
public void queueItem(int itemId)
|
||||||
public void checkPrices()
|
|
||||||
{
|
{
|
||||||
Integer itemId = pendingPriceLookups.poll();
|
if (pendingItems.size() >= MAX_PENDING)
|
||||||
if (itemId == null)
|
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchPrice(itemId);
|
pendingItems.add(itemId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Scheduled(fixedDelay = 5000)
|
@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();
|
String search = pendingSearches.poll();
|
||||||
if (search == null)
|
if (search == null)
|
||||||
{
|
{
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
@@ -413,5 +437,19 @@ public class ItemService
|
|||||||
{
|
{
|
||||||
log.warn("error while searching items", ex);
|
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