From 326bb995b19c205c061827b878d8957ee613db94 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 12 Jan 2024 11:27:55 -0500 Subject: [PATCH] feat: add activitypub request cache --- src/activitypub/index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/activitypub/index.js b/src/activitypub/index.js index 2c6ca9d1c4..fe7f1801c4 100644 --- a/src/activitypub/index.js +++ b/src/activitypub/index.js @@ -9,6 +9,7 @@ const db = require('../database'); const user = require('../user'); const ttl = require('../cache/ttl'); +const requestCache = ttl({ ttl: 1000 * 60 * 5 }); // 5 minutes const actorCache = ttl({ ttl: 1000 * 60 * 60 * 24 }); // 24 hours const ActivityPub = module.exports; @@ -169,6 +170,11 @@ ActivityPub.verify = async (req) => { }; ActivityPub.get = async (uid, uri) => { + const cacheKey = [uid, uri].join(';'); + if (requestCache.has(cacheKey)) { + return requestCache.get(cacheKey); + } + const headers = uid > 0 ? await ActivityPub.sign(uid, uri) : {}; const { response, body } = await request.get(uri, { headers: { @@ -186,6 +192,7 @@ ActivityPub.get = async (uid, uri) => { throw new Error(`[[error:activitypub.get-failed]]`); } + requestCache.set(cacheKey, body); return body; };