mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-24 16:38:41 +03:00
Merge pull request #409 from mchangrh/redisHashCache
getHash redis cache
This commit is contained in:
@@ -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
35
src/utils/getHashCache.ts
Normal 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;
|
||||
}
|
||||
@@ -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}`;
|
||||
}
|
||||
Reference in New Issue
Block a user