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