From f567acb58fdefd489052eb3fb721c9d5e58f07a3 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 6 May 2023 20:30:11 -0500 Subject: [PATCH] Cache rich media cards for 12 hours --- src/deps.ts | 1 + src/transmute.ts | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/deps.ts b/src/deps.ts index 75fe3d8..4cb15bf 100644 --- a/src/deps.ts +++ b/src/deps.ts @@ -30,3 +30,4 @@ import 'npm:linkify-plugin-hashtag@^4.1.0'; // @deno-types="npm:@types/mime@3.0.0" export { default as mime } from 'npm:mime@^3.0.0'; export { unfurl } from 'npm:unfurl.js@^6.3.1'; +export { default as TTLCache } from 'npm:@isaacs/ttlcache@^1.4.0'; diff --git a/src/transmute.ts b/src/transmute.ts index 08f6569..5da97b3 100644 --- a/src/transmute.ts +++ b/src/transmute.ts @@ -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 MetaContent, parseMetaContent } from '@/schema.ts'; @@ -107,7 +107,7 @@ async function toStatus(event: Event<1>) { return { id: event.id, account, - card: firstUrl ? await unfurlCard(firstUrl) : null, + card: firstUrl ? await unfurlCardCached(firstUrl) : null, content: html, created_at: new Date(event.created_at * 1000).toISOString(), in_reply_to_id: replyTag ? replyTag[1] : null, @@ -196,4 +196,19 @@ async function unfurlCard(url: string): Promise { } } +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 { + const cached = previewCardCache.get(url); + if (cached !== undefined) return cached; + + const card = await unfurlCard(url); + previewCardCache.set(url, card); + + return card; +} + export { toAccount, toStatus };