Files
SponsorBlockServer/src/utils/getHashCache.ts
Michael C d1d7675a8c test fixes
test fixes
- fix timeout in redis (by @ajayyy)
- allow "errors" in tempVIP test
- remove duplicate warning in postSkipSegments
- remove duplicate VIP in tempVIP
- run tests against different user once tempVIP removed
- fix typo in getHashCache fetching

syntax and wording
- use standard syntax in redisTest
- fix spacing in getLockReason
- typo in npm script name

test cases
- add getHashCache test case
- add more tests to redisTest

configuration
- update config to use redis timeout
- update docker-compose to use newest pinned version

Co-Authored-By: Ajay Ramachandran <dev@ajay.app>
2022-09-07 02:16:23 -04:00

38 lines
1.2 KiB
TypeScript

import redis from "../utils/redis";
import { shaHashKey } from "../utils/redisKeys";
import { HashedValue } from "../types/hash.model";
import { Logger } from "../utils/logger";
import { getHash } from "../utils/getHash";
const defaultedHashTimes = 5000;
const cachedHashTimes = defaultedHashTimes - 1;
export async function getHashCache<T extends string>(value: T, times = defaultedHashTimes): Promise<T & HashedValue> {
if (times === defaultedHashTimes) {
const hashKey = getHash(value, 1);
const result: HashedValue = await getFromRedis(hashKey);
return result as T & HashedValue;
}
return getHash(value, times);
}
async function getFromRedis<T extends string>(key: HashedValue): Promise<T & HashedValue> {
const redisKey = shaHashKey(key);
try {
const reply = await redis.get(redisKey);
if (reply) {
Logger.debug(`Got data from redis: ${reply}`);
return reply as T & HashedValue;
}
} catch (err) {
Logger.error(err as string);
}
// Otherwise, calculate it
const data = getHash(key, cachedHashTimes);
redis.set(redisKey, data).catch((err) => Logger.error(err));
return data as T & HashedValue;
}