From 732138e745cb935ef2962960c471217c3b248b7e Mon Sep 17 00:00:00 2001 From: John Livingston Date: Fri, 21 Apr 2023 12:25:00 +0200 Subject: [PATCH] Chat Federation: some data caching to reduce disk load. --- server/lib/federation/storage.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/server/lib/federation/storage.ts b/server/lib/federation/storage.ts index e9f82704..715b161f 100644 --- a/server/lib/federation/storage.ts +++ b/server/lib/federation/storage.ts @@ -17,6 +17,8 @@ If a file exists, it means the video has a chat. The file itself contains the JSON LiveChatInfos object. */ +const cache: Map = new Map() + /** * This function stores remote LiveChat infos that are contained in ActivityPub objects. * We store these data for remotes videos. @@ -36,6 +38,8 @@ async function storeVideoLiveChatInfos ( ): Promise { const logger = options.peertubeHelpers.logger + cache.delete(video.url) + const remote = video.remote const filePath = await _getFilePath(options, remote, video.uuid, video.url) if (!filePath) { @@ -48,11 +52,15 @@ async function storeVideoLiveChatInfos ( if (!liveChatInfos) { logger.debug(`${remote ? 'Remote' : 'Local'} video ${video.uuid} has no chat infos, removing if necessary`) await _del(options, filePath) + // Delete the cache again, just in case. + cache.delete(video.url) return } logger.debug(`${remote ? 'Remote' : 'Local'} video ${video.uuid} has chat infos to store`) await _store(options, filePath, liveChatInfos) + // Delete the cache again... in case a read failed because we were writing at the same time. + cache.delete(video.url) } /** @@ -66,17 +74,27 @@ async function getVideoLiveChatInfos ( video: MVideoFullLight | MVideoAP | Video | MVideoThumbnail ): Promise { const logger = options.peertubeHelpers.logger + + const cached = cache.get(video.url) + if (cached !== undefined) { return cached } + const remote = ('remote' in video) ? video.remote : !video.isLocal const filePath = await _getFilePath(options, remote, video.uuid, video.url) if (!filePath) { logger.error('Cant compute the file path for storing liveChat infos for video ' + video.uuid) + cache.set(video.url, false) return false } const content = await _get(options, filePath) - if (content === null) { return false } + if (content === null) { + cache.set(video.url, false) + return false + } // We must sanitize here, in case a previous plugin version did not sanitize enougth. - return sanitizePeertubeLiveChatInfos(content) + const r = sanitizePeertubeLiveChatInfos(content) + cache.set(video.url, r) + return r } async function _getFilePath (