From 4029b1523329f4d02f74872f84dc271f5b2b34d4 Mon Sep 17 00:00:00 2001 From: Ajay Date: Mon, 21 Feb 2022 22:46:13 -0500 Subject: [PATCH] Add query cache for shadowban --- databases/_upgrade_private_6.sql | 2 +- src/routes/getSkipSegments.ts | 7 ++++--- src/utils/redisKeys.ts | 3 +++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/databases/_upgrade_private_6.sql b/databases/_upgrade_private_6.sql index 7edd63c..f7a5f4a 100644 --- a/databases/_upgrade_private_6.sql +++ b/databases/_upgrade_private_6.sql @@ -1,6 +1,6 @@ BEGIN TRANSACTION; -DROP INDEX IF EXISTS "sponsorTimes_hashedIP", "privateDB_sponsorTimes_videoID_v2"; +DROP INDEX IF EXISTS "sponsorTimes_hashedIP", "privateDB_sponsorTimes_videoID_v2"; --!sqlite-ignore UPDATE "config" SET value = 6 WHERE key = 'version'; diff --git a/src/routes/getSkipSegments.ts b/src/routes/getSkipSegments.ts index 07eace0..237a3be 100644 --- a/src/routes/getSkipSegments.ts +++ b/src/routes/getSkipSegments.ts @@ -2,7 +2,7 @@ import { Request, Response } from "express"; import { partition } from "lodash"; import { config } from "../config"; import { db, privateDB } from "../databases/databases"; -import { skipSegmentsHashKey, skipSegmentsKey, skipSegmentGroupsKey } from "../utils/redisKeys"; +import { skipSegmentsHashKey, skipSegmentsKey, skipSegmentGroupsKey, shadowHiddenIPKey } from "../utils/redisKeys"; import { SBRecord } from "../types/lib.model"; import { ActionType, Category, DBSegment, HashedIP, IPAddress, OverlappingSegmentGroup, Segment, SegmentCache, SegmentUUID, Service, VideoData, VideoID, VideoIDHash, Visibility, VotableObject } from "../types/segments.model"; import { getHashCache } from "../utils/getHashCache"; @@ -28,8 +28,9 @@ async function prepareCategorySegments(req: Request, videoID: VideoID, service: if (cache.shadowHiddenSegmentIPs[videoID] === undefined) cache.shadowHiddenSegmentIPs[videoID] = {}; if (cache.shadowHiddenSegmentIPs[videoID][segment.timeSubmitted] === undefined) { const service = getService(req?.query?.service as string); - cache.shadowHiddenSegmentIPs[videoID][segment.timeSubmitted] = await privateDB.prepare("all", 'SELECT "hashedIP" FROM "sponsorTimes" WHERE "videoID" = ? AND "timeSubmitted" = ? AND "service" = ?', - [videoID, segment.timeSubmitted, service]) as { hashedIP: HashedIP }[]; + const fetchData = () => privateDB.prepare("all", 'SELECT "hashedIP" FROM "sponsorTimes" WHERE "videoID" = ? AND "timeSubmitted" = ? AND "service" = ?', + [videoID, segment.timeSubmitted, service]) as Promise<{ hashedIP: HashedIP }[]>; + cache.shadowHiddenSegmentIPs[videoID][segment.timeSubmitted] = await QueryCacher.get(fetchData, shadowHiddenIPKey(videoID, segment.timeSubmitted, service)); } const ipList = cache.shadowHiddenSegmentIPs[videoID][segment.timeSubmitted]; diff --git a/src/utils/redisKeys.ts b/src/utils/redisKeys.ts index 911db8f..2da45a8 100644 --- a/src/utils/redisKeys.ts +++ b/src/utils/redisKeys.ts @@ -16,6 +16,9 @@ export function skipSegmentsHashKey(hashedVideoIDPrefix: VideoIDHash, service: S return `segments.v4.${service}.${hashedVideoIDPrefix}`; } +export const shadowHiddenIPKey = (videoID: VideoID, timeSubmitted: number, service: Service): string => + `segments.${service}.videoID.${videoID}.shadow.${timeSubmitted}`; + export const reputationKey = (userID: UserID): string => `reputation.user.${userID}`;