Cache rich media cards for 12 hours
This commit is contained in:
parent
78b1c24ee0
commit
f567acb58f
|
@ -30,3 +30,4 @@ import 'npm:linkify-plugin-hashtag@^4.1.0';
|
||||||
// @deno-types="npm:@types/mime@3.0.0"
|
// @deno-types="npm:@types/mime@3.0.0"
|
||||||
export { default as mime } from 'npm:mime@^3.0.0';
|
export { default as mime } from 'npm:mime@^3.0.0';
|
||||||
export { unfurl } from 'npm:unfurl.js@^6.3.1';
|
export { unfurl } from 'npm:unfurl.js@^6.3.1';
|
||||||
|
export { default as TTLCache } from 'npm:@isaacs/ttlcache@^1.4.0';
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { findReplyTag, lodash, nip19, unfurl, z } from '@/deps.ts';
|
import { findReplyTag, lodash, nip19, TTLCache, unfurl, z } from '@/deps.ts';
|
||||||
import { type Event } from '@/event.ts';
|
import { type Event } from '@/event.ts';
|
||||||
import { type MetaContent, parseMetaContent } from '@/schema.ts';
|
import { type MetaContent, parseMetaContent } from '@/schema.ts';
|
||||||
|
|
||||||
|
@ -107,7 +107,7 @@ async function toStatus(event: Event<1>) {
|
||||||
return {
|
return {
|
||||||
id: event.id,
|
id: event.id,
|
||||||
account,
|
account,
|
||||||
card: firstUrl ? await unfurlCard(firstUrl) : null,
|
card: firstUrl ? await unfurlCardCached(firstUrl) : null,
|
||||||
content: html,
|
content: html,
|
||||||
created_at: new Date(event.created_at * 1000).toISOString(),
|
created_at: new Date(event.created_at * 1000).toISOString(),
|
||||||
in_reply_to_id: replyTag ? replyTag[1] : null,
|
in_reply_to_id: replyTag ? replyTag[1] : null,
|
||||||
|
@ -196,4 +196,19 @@ async function unfurlCard(url: string): Promise<PreviewCard | null> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const TWELVE_HOURS = 12 * 60 * 60 * 1000;
|
||||||
|
|
||||||
|
const previewCardCache = new TTLCache({ ttl: TWELVE_HOURS, max: 500 });
|
||||||
|
|
||||||
|
/** Unfurl card from cache if available, otherwise fetch it. */
|
||||||
|
async function unfurlCardCached(url: string): Promise<PreviewCard | null> {
|
||||||
|
const cached = previewCardCache.get<PreviewCard | null>(url);
|
||||||
|
if (cached !== undefined) return cached;
|
||||||
|
|
||||||
|
const card = await unfurlCard(url);
|
||||||
|
previewCardCache.set(url, card);
|
||||||
|
|
||||||
|
return card;
|
||||||
|
}
|
||||||
|
|
||||||
export { toAccount, toStatus };
|
export { toAccount, toStatus };
|
||||||
|
|
Loading…
Reference in New Issue