Merge pull request #409 from mchangrh/redisHashCache

getHash redis cache
This commit is contained in:
Ajay Ramachandran
2021-12-03 00:14:01 -05:00
committed by GitHub
25 changed files with 93 additions and 48 deletions

View File

@@ -10,4 +10,4 @@ export function getHash<T extends string>(value: T, times = 5000): T & HashedVal
}
return value as T & HashedValue;
}
}

35
src/utils/getHashCache.ts Normal file
View File

@@ -0,0 +1,35 @@
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);
const { err, reply } = await redis.getAsync(redisKey);
if (!err && reply) {
try {
Logger.debug(`Got data from redis: ${reply}`);
return reply as T & HashedValue;
} catch (e) {
// If all else, continue on hashing
}
}
const data = getHash(key, cachedHashTimes);
redis.setAsync(key, data);
return data as T & HashedValue;
}

View File

@@ -1,5 +1,6 @@
import { Service, VideoID, VideoIDHash } from "../types/segments.model";
import { UserID } from "../types/user.model";
import { HashedValue } from "../types/hash.model";
import { Logger } from "./logger";
export function skipSegmentsKey(videoID: VideoID, service: Service): string {
@@ -22,4 +23,10 @@ export function ratingHashKey(hashPrefix: VideoIDHash, service: Service): string
if (hashPrefix.length !== 4) Logger.warn(`Redis rating hash-prefix key is not length 4! ${hashPrefix}`);
return `rating.${service}.${hashPrefix}`;
}
export function shaHashKey(singleIter: HashedValue): string {
if (singleIter.length !== 64) Logger.warn(`Redis sha.hash key is not length 64! ${singleIter}`);
return `sha.hash.${singleIter}`;
}