feed controller: add etag
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user