Add default expiry to query cache

Resolves #461
This commit is contained in:
Ajay
2022-08-11 21:37:13 -04:00
parent 877c3b7107
commit 479890fc0d

View File

@@ -4,6 +4,8 @@ import { skipSegmentsHashKey, skipSegmentsKey, reputationKey, ratingHashKey, ski
import { Service, VideoID, VideoIDHash } from "../types/segments.model"; import { Service, VideoID, VideoIDHash } from "../types/segments.model";
import { Feature, HashedUserID, UserID } from "../types/user.model"; import { Feature, HashedUserID, UserID } from "../types/user.model";
const expiryTime = 2 * 60 * 60;
async function get<T>(fetchFromDB: () => Promise<T>, key: string): Promise<T> { async function get<T>(fetchFromDB: () => Promise<T>, key: string): Promise<T> {
try { try {
const reply = await redis.get(key); const reply = await redis.get(key);
@@ -16,7 +18,7 @@ async function get<T>(fetchFromDB: () => Promise<T>, key: string): Promise<T> {
const data = await fetchFromDB(); const data = await fetchFromDB();
redis.set(key, JSON.stringify(data)).catch((err) => Logger.error(err)); redis.setEx(key, expiryTime, JSON.stringify(data)).catch((err) => Logger.error(err));
return data; return data;
} }
@@ -67,7 +69,7 @@ async function getAndSplit<T, U extends string>(fetchFromDB: (values: U[]) => Pr
} }
for (const key in newResults) { for (const key in newResults) {
redis.set(key, JSON.stringify(newResults[key])).catch((err) => Logger.error(err)); redis.setEx(key, expiryTime, JSON.stringify(newResults[key])).catch((err) => Logger.error(err));
} }
}); });
} }