forked from WikiDeck/wikideck
More cards
This commit is contained in:
+10
-1
@@ -53,10 +53,19 @@ export class WikipediaEnrichment {
|
||||
}
|
||||
|
||||
async get(pageId: number): Promise<PageEnrichment> {
|
||||
return this.loadCached(pageId, false);
|
||||
}
|
||||
|
||||
/** Bypass cached values when the result is being pinned into a newly minted block. */
|
||||
async getFresh(pageId: number): Promise<PageEnrichment> {
|
||||
return this.loadCached(pageId, true);
|
||||
}
|
||||
|
||||
private async loadCached(pageId: number, fresh: boolean): Promise<PageEnrichment> {
|
||||
if (!Number.isSafeInteger(pageId) || pageId <= 0) throw new RangeError('Invalid Wikipedia page ID');
|
||||
const now = this.now();
|
||||
const cached = this.cache.get(pageId);
|
||||
if (cached && cached.expires > now.getTime() &&
|
||||
if (!fresh && cached && cached.expires > now.getTime() &&
|
||||
(!('value' in cached) || cached.value.pageviews.latestMonth === monthWindow(now)[5])) {
|
||||
this.cache.delete(pageId);
|
||||
this.cache.set(pageId, cached);
|
||||
|
||||
Reference in New Issue
Block a user