From 794fd57b856a2f42c6ddf2db0e7cd98c708da8bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Caras?= Date: Thu, 23 Nov 2023 19:24:47 +0100 Subject: [PATCH] feat(mastodon): :zap: add caching to mastodon --- .vscode/settings.json | 5 ++++- src/api/mastodon.ts | 13 +++++++++++-- src/server.ts | 9 +++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 3662b37..995e934 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,6 @@ { - "typescript.tsdk": "node_modules/typescript/lib" + "typescript.tsdk": "node_modules/typescript/lib", + "conventionalCommits.scopes": [ + "mastodon" + ] } \ No newline at end of file diff --git a/src/api/mastodon.ts b/src/api/mastodon.ts index 5771d45..ee4d3a8 100644 --- a/src/api/mastodon.ts +++ b/src/api/mastodon.ts @@ -1,4 +1,5 @@ import axios from 'axios'; +import { cache } from '../server'; async function findMastodonUser(username: string, host: string): Promise { const res = await axios.get(`${host}/api/v2/search?q=${username}`, { @@ -26,6 +27,11 @@ interface MastodonPost { } async function getLatestMastodonPost(id: string, host: string): Promise { + const cached = cache.get('mastodon') as MastodonPost | undefined; + if (cached != undefined) { + console.info('Returning mastodon from cache'); + return cached; + } const res = await axios.get( `${host}/api/v1/accounts/${id}/statuses?limit=1&exclude_reblogs=true`, { @@ -47,7 +53,7 @@ async function getLatestMastodonPost(id: string, host: string): Promise