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

View File

@@ -1,7 +1,7 @@
import { db } from "../databases/databases";
import { UserID } from "../types/user.model";
import { QueryCacher } from "./queryCacher";
import { userKey } from "./redisKeys";
import { reputationKey } from "./redisKeys";
interface ReputationDBResult {
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"
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
if (result.totalSubmissions < 5) {