item service: always use last known price, and queue price updates instead

This commit is contained in:
Adam
2018-02-16 23:28:03 -05:00
parent 4bcfdc5360
commit 77a3f48627
4 changed files with 51 additions and 15 deletions

View File

@@ -44,11 +44,13 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.sql2o.Sql2o; import org.sql2o.Sql2o;
import org.sql2o.converters.Converter; import org.sql2o.converters.Converter;
import org.sql2o.quirks.NoQuirks; import org.sql2o.quirks.NoQuirks;
@SpringBootApplication @SpringBootApplication
@EnableScheduling
@Slf4j @Slf4j
public class SpringBootWebApplication extends SpringBootServletInitializer public class SpringBootWebApplication extends SpringBootServletInitializer
{ {

View File

@@ -176,12 +176,9 @@ public class ItemController
.build(); .build();
} }
} }
else else if (priceEntry == null)
{ {
Instant cacheTime = now.minus(CACHE_DUATION); // Price is unknown
if (priceEntry == null || priceEntry.getFetched_time().isBefore(cacheTime))
{
// Price is unknown, or was fetched too long ago
List<PriceEntry> prices = itemService.fetchPrice(itemId); List<PriceEntry> prices = itemService.fetchPrice(itemId);
if (prices == null || prices.isEmpty()) if (prices == null || prices.isEmpty())
@@ -196,6 +193,14 @@ public class ItemController
priceEntry = prices.get(prices.size() - 1); priceEntry = prices.get(prices.size() - 1);
hit = false; hit = false;
} }
else
{
Instant cacheTime = now.minus(CACHE_DUATION);
if (priceEntry.getFetched_time().isBefore(cacheTime))
{
// Queue a check for the price
itemService.queueLookup(itemId);
}
} }
ItemPrice itemPrice = new ItemPrice(); ItemPrice itemPrice = new ItemPrice();

View File

@@ -32,6 +32,7 @@ import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.runelite.http.api.RuneLiteAPI; import net.runelite.http.api.RuneLiteAPI;
import net.runelite.http.api.item.ItemType; import net.runelite.http.api.item.ItemType;
@@ -40,6 +41,7 @@ import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
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.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.sql2o.Connection; import org.sql2o.Connection;
import org.sql2o.Query; import org.sql2o.Query;
@@ -78,7 +80,10 @@ public class ItemService
private static final String CREATE_PRICES_FK = "ALTER TABLE `prices`\n" private static final String CREATE_PRICES_FK = "ALTER TABLE `prices`\n"
+ " ADD CONSTRAINT `item` FOREIGN KEY (`item`) REFERENCES `items` (`id`);"; + " ADD CONSTRAINT `item` FOREIGN KEY (`item`) REFERENCES `items` (`id`);";
private static final int MAX_PENDING_LOOKUPS = 512;
private final Sql2o sql2o; private final Sql2o sql2o;
private final ConcurrentLinkedQueue<Integer> pendingPriceLookups = new ConcurrentLinkedQueue<>();
@Autowired @Autowired
public ItemService(@Qualifier("Runelite SQL2O") Sql2o sql2o) public ItemService(@Qualifier("Runelite SQL2O") Sql2o sql2o)
@@ -339,4 +344,26 @@ public class ItemService
return response.body().bytes(); return response.body().bytes();
} }
} }
public void queueLookup(int itemId)
{
if (pendingPriceLookups.size() >= MAX_PENDING_LOOKUPS)
{
return;
}
pendingPriceLookups.add(itemId);
}
@Scheduled(fixedDelay = 5000)
public void checkPrices()
{
Integer itemId = pendingPriceLookups.poll();
if (itemId == null)
{
return;
}
fetchPrice(itemId);
}
} }

View File

@@ -34,11 +34,13 @@ import org.junit.Test;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.sql2o.Sql2o; import org.sql2o.Sql2o;
import org.sql2o.converters.Converter; import org.sql2o.converters.Converter;
import org.sql2o.quirks.NoQuirks; import org.sql2o.quirks.NoQuirks;
@SpringBootApplication @SpringBootApplication
@EnableScheduling
public class SpringBootWebApplicationTest public class SpringBootWebApplicationTest
{ {
@Bean("Runelite SQL2O") @Bean("Runelite SQL2O")