Merge remote-tracking branch 'runelite/master' into 2009-merge
This commit is contained in:
@@ -24,6 +24,9 @@
|
||||
*/
|
||||
package net.runelite.http.service.feed;
|
||||
|
||||
import com.google.common.hash.HashCode;
|
||||
import com.google.common.hash.Hasher;
|
||||
import com.google.common.hash.Hashing;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -51,7 +54,26 @@ public class FeedController
|
||||
private final TwitterService twitterService;
|
||||
private final OSRSNewsService osrsNewsService;
|
||||
|
||||
private FeedResult feedResult;
|
||||
private static class MemoizedFeed
|
||||
{
|
||||
final FeedResult feedResult;
|
||||
final String hash;
|
||||
|
||||
MemoizedFeed(FeedResult feedResult)
|
||||
{
|
||||
this.feedResult = feedResult;
|
||||
|
||||
Hasher hasher = Hashing.sha256().newHasher();
|
||||
for (FeedItem itemPrice : feedResult.getItems())
|
||||
{
|
||||
hasher.putBytes(itemPrice.getTitle().getBytes()).putBytes(itemPrice.getContent().getBytes());
|
||||
}
|
||||
HashCode code = hasher.hash();
|
||||
hash = code.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private MemoizedFeed memoizedFeed;
|
||||
|
||||
@Autowired
|
||||
public FeedController(BlogService blogService, TwitterService twitterService, OSRSNewsService osrsNewsService)
|
||||
@@ -93,20 +115,21 @@ public class FeedController
|
||||
log.warn(e.getMessage());
|
||||
}
|
||||
|
||||
feedResult = new FeedResult(items);
|
||||
memoizedFeed = new MemoizedFeed(new FeedResult(items));
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<FeedResult> getFeed()
|
||||
{
|
||||
if (feedResult == null)
|
||||
if (memoizedFeed == null)
|
||||
{
|
||||
return ResponseEntity.notFound()
|
||||
.build();
|
||||
}
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.eTag(memoizedFeed.hash)
|
||||
.cacheControl(CacheControl.maxAge(10, TimeUnit.MINUTES).cachePublic())
|
||||
.body(feedResult);
|
||||
.body(memoizedFeed.feedResult);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,9 @@ import com.google.common.base.Supplier;
|
||||
import com.google.common.base.Suppliers;
|
||||
import com.google.common.cache.Cache;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.hash.HashCode;
|
||||
import com.google.common.hash.Hasher;
|
||||
import com.google.common.hash.Hashing;
|
||||
import java.time.Instant;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -53,20 +56,39 @@ public class ItemController
|
||||
private static final String RUNELITE_CACHE = "RuneLite-Cache";
|
||||
private static final int MAX_BATCH_LOOKUP = 1024;
|
||||
|
||||
private static class MemoizedPrices
|
||||
{
|
||||
final ItemPrice[] prices;
|
||||
final String hash;
|
||||
|
||||
MemoizedPrices(ItemPrice[] prices)
|
||||
{
|
||||
this.prices = prices;
|
||||
|
||||
Hasher hasher = Hashing.sha256().newHasher();
|
||||
for (ItemPrice itemPrice : prices)
|
||||
{
|
||||
hasher.putInt(itemPrice.getId()).putInt(itemPrice.getPrice());
|
||||
}
|
||||
HashCode code = hasher.hash();
|
||||
hash = code.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private final Cache<Integer, Integer> cachedEmpty = CacheBuilder.newBuilder()
|
||||
.maximumSize(1024L)
|
||||
.build();
|
||||
|
||||
private final ItemService itemService;
|
||||
|
||||
private final Supplier<ItemPrice[]> memorizedPrices;
|
||||
private final Supplier<MemoizedPrices> memoizedPrices;
|
||||
|
||||
@Autowired
|
||||
public ItemController(ItemService itemService)
|
||||
{
|
||||
this.itemService = itemService;
|
||||
|
||||
memorizedPrices = Suppliers.memoizeWithExpiration(() -> itemService.fetchPrices().stream()
|
||||
memoizedPrices = Suppliers.memoizeWithExpiration(() -> new MemoizedPrices(itemService.fetchPrices().stream()
|
||||
.map(priceEntry ->
|
||||
{
|
||||
ItemPrice itemPrice = new ItemPrice();
|
||||
@@ -76,7 +98,7 @@ public class ItemController
|
||||
itemPrice.setTime(priceEntry.getTime());
|
||||
return itemPrice;
|
||||
})
|
||||
.toArray(ItemPrice[]::new), 30, TimeUnit.MINUTES);
|
||||
.toArray(ItemPrice[]::new)), 30, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
@GetMapping("/{itemId}")
|
||||
@@ -220,8 +242,10 @@ public class ItemController
|
||||
@GetMapping("/prices")
|
||||
public ResponseEntity<ItemPrice[]> prices()
|
||||
{
|
||||
MemoizedPrices memorizedPrices = this.memoizedPrices.get();
|
||||
return ResponseEntity.ok()
|
||||
.eTag(memorizedPrices.hash)
|
||||
.cacheControl(CacheControl.maxAge(30, TimeUnit.MINUTES).cachePublic())
|
||||
.body(memorizedPrices.get());
|
||||
.body(memorizedPrices.prices);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user