From 9c3d619ad5e9d3c55e8848e1f023121cf985507b Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Fri, 17 May 2024 16:22:20 +0800 Subject: [PATCH] put locale and timezone in cache key --- packages/content-fetch/src/request_handler.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/content-fetch/src/request_handler.ts b/packages/content-fetch/src/request_handler.ts index 403b9255d..8172673bb 100644 --- a/packages/content-fetch/src/request_handler.ts +++ b/packages/content-fetch/src/request_handler.ts @@ -84,31 +84,29 @@ const uploadOriginalContent = async ( ) } -const cacheKey = (url: string) => `fetch-result:${url}` +const cacheKey = (url: string, locale = '', timezone = '') => + `fetch-result:${url}:${locale}:${timezone}` const isFetchResult = (obj: unknown): obj is FetchResult => { return typeof obj === 'object' && obj !== null && 'finalUrl' in obj } export const cacheFetchResult = async ( - url: string, + key: string, fetchResult: FetchResult ) => { // cache the fetch result for 24 hours const ttl = 24 * 60 * 60 - const key = cacheKey(url) const value = JSON.stringify(fetchResult) return redisDataSource.cacheClient.set(key, value, 'EX', ttl, 'NX') } const getCachedFetchResult = async ( - url: string + key: string ): Promise => { - const key = cacheKey(url) - const result = await redisDataSource.cacheClient.get(key) if (!result) { - console.info('fetch result is not cached', url) + console.info('fetch result is not cached', key) return null } @@ -117,7 +115,7 @@ const getCachedFetchResult = async ( throw new Error('fetch result is not valid') } - console.info('fetch result is cached', url) + console.info('fetch result is cached', key) return fetchResult } @@ -173,7 +171,8 @@ export const contentFetchRequestHandler: RequestHandler = async (req, res) => { console.log(`Article parsing request`, logRecord) try { - let fetchResult = await getCachedFetchResult(url) + const key = cacheKey(url, locale, timezone) + let fetchResult = await getCachedFetchResult(key) if (!fetchResult) { console.log( 'fetch result not found in cache, fetching content now...',