From 0cd25f0498e240434172b4a25502eae39815d81b Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Thu, 2 Dec 2021 23:42:39 -0500 Subject: [PATCH] Fix issues with query caching for ratings --- src/utils/queryCacher.ts | 44 +++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/src/utils/queryCacher.ts b/src/utils/queryCacher.ts index 34fc1a0..5ddccef 100644 --- a/src/utils/queryCacher.ts +++ b/src/utils/queryCacher.ts @@ -27,7 +27,7 @@ async function get(fetchFromDB: () => Promise, key: string): Promise { /** * Gets from redis for all specified values and splits the result before adding it to redis cache */ -async function getAndSplit(fetchFromDB: (values: U[]) => Promise>, keyGenerator: (value: U) => string, splitKey: string, values: U[]): Promise> { +async function getAndSplit(fetchFromDB: (values: U[]) => Promise>, keyGenerator: (value: U) => string, splitKey: string, values: U[]): Promise> { const cachedValues = await Promise.all(values.map(async (value) => { const key = keyGenerator(value); const { err, reply } = await redis.getAsync(key); @@ -49,24 +49,36 @@ async function getAndSplit(fetchFromDB: (values: U[]) => Promise> }; })); - const data = await fetchFromDB( - cachedValues.filter((cachedValue) => cachedValue.result === null) - .map((cachedValue) => cachedValue.value)); + const valuesToBeFetched = cachedValues.filter((cachedValue) => cachedValue.result === null) + .map((cachedValue) => cachedValue.value); - new Promise(() => { - const newResults: Record = {}; - for (const item of data) { - const key = (item as unknown as Record)[splitKey]; - newResults[key] ??= []; - newResults[key].push(item); - } + let data: Array = []; + if (valuesToBeFetched.length > 0) { + data = await fetchFromDB(valuesToBeFetched); - for (const key in newResults) { - redis.setAsync(keyGenerator(key as unknown as U), JSON.stringify(newResults[key])); - } - }); + new Promise(() => { + const newResults: Record = {}; + for (const item of data) { + const splitValue = (item as unknown as Record)[splitKey]; + const key = keyGenerator(splitValue as unknown as U); + newResults[key] ??= []; + newResults[key].push(item); + } - return data.concat(cachedValues.map((cachedValue) => cachedValue.result).filter((result) => result !== null)); + for (const value of valuesToBeFetched) { + // If it wasn't in the result, cache it as blank + newResults[keyGenerator(value)] ??= []; + } + + console.log(newResults); + + for (const key in newResults) { + redis.setAsync(key, JSON.stringify(newResults[key])); + } + }); + } + + return data.concat(...(cachedValues.map((cachedValue) => cachedValue.result).filter((result) => result !== null) || [])); } function clearSegmentCache(videoInfo: { videoID: VideoID; hashedVideoID: VideoIDHash; service: Service; userID?: UserID; }): void {