Allow disabling hash cache

This commit is contained in:
Ajay
2022-11-19 20:05:59 -05:00
parent 9e2e1343da
commit 849ca52ef8
3 changed files with 17 additions and 9 deletions

View File

@@ -140,7 +140,8 @@ addDefaults(config, {
expiryTime: 24 * 60 * 60,
getTimeout: 40,
maxConnections: 15000,
maxWriteConnections: 1000
maxWriteConnections: 1000,
disableHashCache: false
},
redisRead: {
enabled: false,

View File

@@ -7,6 +7,7 @@ interface RedisConfig extends redis.RedisClientOptions {
getTimeout: number;
maxConnections: number;
maxWriteConnections: number;
disableHashCache: boolean;
}
interface RedisReadOnlyConfig extends redis.RedisClientOptions {

View File

@@ -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;
}