Clear reputation cache

This commit is contained in:
Ajay Ramachandran
2021-05-23 17:05:06 -04:00
parent a5f9c2a022
commit 194c657ba7
5 changed files with 14 additions and 11 deletions

View File

@@ -1,7 +1,8 @@
import redis from "../utils/redis"; import redis from "../utils/redis";
import { Logger } from "../utils/logger"; import { Logger } from "../utils/logger";
import { skipSegmentsHashKey, skipSegmentsKey } from "./redisKeys"; import { skipSegmentsHashKey, skipSegmentsKey, reputationKey } from "./redisKeys";
import { Service, VideoID, VideoIDHash } from "../types/segments.model"; import { Service, VideoID, VideoIDHash } from "../types/segments.model";
import { UserID } from "../types/user.model";
async function get<T>(fetchFromDB: () => Promise<T>, key: string): Promise<T> { async function get<T>(fetchFromDB: () => Promise<T>, key: string): Promise<T> {
const {err, reply} = await redis.getAsync(key); const {err, reply} = await redis.getAsync(key);
@@ -21,10 +22,11 @@ async function get<T>(fetchFromDB: () => Promise<T>, key: string): Promise<T> {
return data; return data;
} }
function clearVideoCache(videoInfo: { videoID: VideoID; hashedVideoID: VideoIDHash; service: Service; }) { function clearVideoCache(videoInfo: { videoID: VideoID; hashedVideoID: VideoIDHash; service: Service; userID: UserID; }) {
if (videoInfo) { if (videoInfo) {
redis.delAsync(skipSegmentsKey(videoInfo.videoID, videoInfo.service)); redis.delAsync(skipSegmentsKey(videoInfo.videoID, videoInfo.service));
redis.delAsync(skipSegmentsHashKey(videoInfo.hashedVideoID, videoInfo.service)); redis.delAsync(skipSegmentsHashKey(videoInfo.hashedVideoID, videoInfo.service));
redis.delAsync(reputationKey(videoInfo.userID));
} }
} }

View File

@@ -13,6 +13,6 @@ export function skipSegmentsHashKey(hashedVideoIDPrefix: VideoIDHash, service: S
return "segments." + service + "." + hashedVideoIDPrefix; return "segments." + service + "." + hashedVideoIDPrefix;
} }
export function userKey(userID: UserID): string { export function reputationKey(userID: UserID): string {
return "user." + userID; return "reputation.user." + userID;
} }

View File

@@ -1,7 +1,7 @@
import { db } from "../databases/databases"; import { db } from "../databases/databases";
import { UserID } from "../types/user.model"; import { UserID } from "../types/user.model";
import { QueryCacher } from "./queryCacher"; import { QueryCacher } from "./queryCacher";
import { userKey } from "./redisKeys"; import { reputationKey } from "./redisKeys";
interface ReputationDBResult { interface ReputationDBResult {
totalSubmissions: number, totalSubmissions: number,
@@ -19,7 +19,7 @@ export async function getReputation(userID: UserID) {
SUM(CASE WHEN "timeSubmitted" < ? AND "votes" > 0 THEN 1 ELSE 0 END) AS "oldUpvotedSubmissions" SUM(CASE WHEN "timeSubmitted" < ? AND "votes" > 0 THEN 1 ELSE 0 END) AS "oldUpvotedSubmissions"
FROM "sponsorTimes" WHERE "userID" = ?`, [pastDate, userID]) as Promise<ReputationDBResult>; FROM "sponsorTimes" WHERE "userID" = ?`, [pastDate, userID]) as Promise<ReputationDBResult>;
const result = await QueryCacher.get(fetchFromDB, userKey(userID)); const result = await QueryCacher.get(fetchFromDB, reputationKey(userID));
// Grace period // Grace period
if (result.totalSubmissions < 5) { if (result.totalSubmissions < 5) {

View File

@@ -532,7 +532,8 @@ export async function postSkipSegments(req: Request, res: Response) {
QueryCacher.clearVideoCache({ QueryCacher.clearVideoCache({
videoID, videoID,
hashedVideoID, hashedVideoID,
service service,
userID
}); });
} catch (err) { } catch (err) {
//a DB change probably occurred //a DB change probably occurred

View File

@@ -156,8 +156,8 @@ async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, i
return; return;
} }
const videoInfo = (await db.prepare('get', `SELECT "category", "videoID", "hashedVideoID", "service" FROM "sponsorTimes" WHERE "UUID" = ?`, const videoInfo = (await db.prepare('get', `SELECT "category", "videoID", "hashedVideoID", "service", "userID" FROM "sponsorTimes" WHERE "UUID" = ?`,
[UUID])) as {category: Category, videoID: VideoID, hashedVideoID: VideoIDHash, service: Service}; [UUID])) as {category: Category, videoID: VideoID, hashedVideoID: VideoIDHash, service: Service, userID: UserID};
if (!videoInfo) { if (!videoInfo) {
// Submission doesn't exist // Submission doesn't exist
res.status(400).send("Submission doesn't exist."); res.status(400).send("Submission doesn't exist.");
@@ -364,8 +364,8 @@ export async function voteOnSponsorTime(req: Request, res: Response) {
} }
//check if the increment amount should be multiplied (downvotes have more power if there have been many views) //check if the increment amount should be multiplied (downvotes have more power if there have been many views)
const videoInfo = await db.prepare('get', `SELECT "videoID", "hashedVideoID", "service", "votes", "views" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]) as const videoInfo = await db.prepare('get', `SELECT "videoID", "hashedVideoID", "service", "votes", "views", "userID" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]) as
{videoID: VideoID, hashedVideoID: VideoIDHash, service: Service, votes: number, views: number}; {videoID: VideoID, hashedVideoID: VideoIDHash, service: Service, votes: number, views: number, userID: UserID};
if (voteTypeEnum === voteTypes.normal) { if (voteTypeEnum === voteTypes.normal) {
if ((isVIP || isOwnSubmission) && incrementAmount < 0) { if ((isVIP || isOwnSubmission) && incrementAmount < 0) {