mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-06 19:47:00 +03:00
Allow disabling hash cache
This commit is contained in:
@@ -140,7 +140,8 @@ addDefaults(config, {
|
||||
expiryTime: 24 * 60 * 60,
|
||||
getTimeout: 40,
|
||||
maxConnections: 15000,
|
||||
maxWriteConnections: 1000
|
||||
maxWriteConnections: 1000,
|
||||
disableHashCache: false
|
||||
},
|
||||
redisRead: {
|
||||
enabled: false,
|
||||
|
||||
@@ -7,6 +7,7 @@ interface RedisConfig extends redis.RedisClientOptions {
|
||||
getTimeout: number;
|
||||
maxConnections: number;
|
||||
maxWriteConnections: number;
|
||||
disableHashCache: boolean;
|
||||
}
|
||||
|
||||
interface RedisReadOnlyConfig extends redis.RedisClientOptions {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { shaHashKey } from "../utils/redisKeys";
|
||||
import { HashedValue } from "../types/hash.model";
|
||||
import { Logger } from "../utils/logger";
|
||||
import { getHash } from "../utils/getHash";
|
||||
import { config } from "../config";
|
||||
|
||||
const defaultedHashTimes = 5000;
|
||||
const cachedHashTimes = defaultedHashTimes - 1;
|
||||
@@ -19,20 +20,25 @@ export async function getHashCache<T extends string>(value: T, times = defaulted
|
||||
async function getFromRedis<T extends string>(key: HashedValue): Promise<T & HashedValue> {
|
||||
const redisKey = shaHashKey(key);
|
||||
|
||||
try {
|
||||
const reply = await redis.get(redisKey);
|
||||
if (!config.redis?.disableHashCache) {
|
||||
try {
|
||||
const reply = await redis.get(redisKey);
|
||||
|
||||
if (reply) {
|
||||
Logger.debug(`Got data from redis: ${reply}`);
|
||||
return reply as T & HashedValue;
|
||||
if (reply) {
|
||||
Logger.debug(`Got data from redis: ${reply}`);
|
||||
return reply as T & HashedValue;
|
||||
}
|
||||
} catch (err) {
|
||||
Logger.error(err as string);
|
||||
}
|
||||
} catch (err) {
|
||||
Logger.error(err as string);
|
||||
}
|
||||
|
||||
// Otherwise, calculate it
|
||||
const data = getHash(key, cachedHashTimes);
|
||||
redis.set(redisKey, data).catch((err) => Logger.error(err));
|
||||
|
||||
if (!config.redis?.disableHashCache) {
|
||||
redis.set(redisKey, data).catch((err) => Logger.error(err));
|
||||
}
|
||||
|
||||
return data as T & HashedValue;
|
||||
}
|
||||
Reference in New Issue
Block a user