item manager: use caching okhttp client
This commit is contained in:
@@ -34,8 +34,10 @@ import java.lang.reflect.Type;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
import javax.inject.Inject;
|
||||||
import net.runelite.http.api.RuneLiteAPI;
|
import net.runelite.http.api.RuneLiteAPI;
|
||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -45,6 +47,14 @@ public class ItemClient
|
|||||||
{
|
{
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ItemClient.class);
|
private static final Logger logger = LoggerFactory.getLogger(ItemClient.class);
|
||||||
|
|
||||||
|
private final OkHttpClient client;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public ItemClient(OkHttpClient client)
|
||||||
|
{
|
||||||
|
this.client = client;
|
||||||
|
}
|
||||||
|
|
||||||
public ItemPrice lookupItemPrice(int itemId) throws IOException
|
public ItemPrice lookupItemPrice(int itemId) throws IOException
|
||||||
{
|
{
|
||||||
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
|
HttpUrl url = RuneLiteAPI.getApiBase().newBuilder()
|
||||||
@@ -59,7 +69,7 @@ public class ItemClient
|
|||||||
.url(url)
|
.url(url)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
|
try (Response response = client.newCall(request).execute())
|
||||||
{
|
{
|
||||||
if (!response.isSuccessful())
|
if (!response.isSuccessful())
|
||||||
{
|
{
|
||||||
@@ -95,7 +105,7 @@ public class ItemClient
|
|||||||
.url(url)
|
.url(url)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
|
try (Response response = client.newCall(request).execute())
|
||||||
{
|
{
|
||||||
if (!response.isSuccessful())
|
if (!response.isSuccessful())
|
||||||
{
|
{
|
||||||
@@ -126,7 +136,7 @@ public class ItemClient
|
|||||||
.url(url)
|
.url(url)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
|
try (Response response = client.newCall(request).execute())
|
||||||
{
|
{
|
||||||
if (!response.isSuccessful())
|
if (!response.isSuccessful())
|
||||||
{
|
{
|
||||||
@@ -156,7 +166,7 @@ public class ItemClient
|
|||||||
.url(url)
|
.url(url)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
|
try (Response response = client.newCall(request).execute())
|
||||||
{
|
{
|
||||||
if (!response.isSuccessful())
|
if (!response.isSuccessful())
|
||||||
{
|
{
|
||||||
@@ -187,7 +197,7 @@ public class ItemClient
|
|||||||
.url(url)
|
.url(url)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
|
try (Response response = client.newCall(request).execute())
|
||||||
{
|
{
|
||||||
if (!response.isSuccessful())
|
if (!response.isSuccessful())
|
||||||
{
|
{
|
||||||
@@ -219,7 +229,7 @@ public class ItemClient
|
|||||||
.url(url)
|
.url(url)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute())
|
try (Response response = client.newCall(request).execute())
|
||||||
{
|
{
|
||||||
if (!response.isSuccessful())
|
if (!response.isSuccessful())
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ public class ItemManager
|
|||||||
private final ScheduledExecutorService scheduledExecutorService;
|
private final ScheduledExecutorService scheduledExecutorService;
|
||||||
private final ClientThread clientThread;
|
private final ClientThread clientThread;
|
||||||
|
|
||||||
private final ItemClient itemClient = new ItemClient();
|
private final ItemClient itemClient;
|
||||||
private Map<Integer, ItemPrice> itemPrices = Collections.emptyMap();
|
private Map<Integer, ItemPrice> itemPrices = Collections.emptyMap();
|
||||||
private Map<Integer, ItemStats> itemStats = Collections.emptyMap();
|
private Map<Integer, ItemStats> itemStats = Collections.emptyMap();
|
||||||
private final LoadingCache<ImageKey, AsyncBufferedImage> itemImages;
|
private final LoadingCache<ImageKey, AsyncBufferedImage> itemImages;
|
||||||
@@ -155,11 +155,13 @@ public class ItemManager
|
|||||||
build();
|
build();
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public ItemManager(Client client, ScheduledExecutorService executor, ClientThread clientThread)
|
public ItemManager(Client client, ScheduledExecutorService executor, ClientThread clientThread,
|
||||||
|
ItemClient itemClient)
|
||||||
{
|
{
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.scheduledExecutorService = executor;
|
this.scheduledExecutorService = executor;
|
||||||
this.clientThread = clientThread;
|
this.clientThread = clientThread;
|
||||||
|
this.itemClient = itemClient;
|
||||||
|
|
||||||
scheduledExecutorService.scheduleWithFixedDelay(this::loadPrices, 0, 30, TimeUnit.MINUTES);
|
scheduledExecutorService.scheduleWithFixedDelay(this::loadPrices, 0, 30, TimeUnit.MINUTES);
|
||||||
scheduledExecutorService.submit(this::loadStats);
|
scheduledExecutorService.submit(this::loadStats);
|
||||||
|
|||||||
Reference in New Issue
Block a user