diff --git a/src/config.ts b/src/config.ts index 5ea715e..6a939f1 100644 --- a/src/config.ts +++ b/src/config.ts @@ -140,7 +140,8 @@ addDefaults(config, { expiryTime: 24 * 60 * 60, getTimeout: 40, maxConnections: 15000, - maxWriteConnections: 1000 + maxWriteConnections: 1000, + disableHashCache: false }, redisRead: { enabled: false, diff --git a/src/types/config.model.ts b/src/types/config.model.ts index 459d231..08729d8 100644 --- a/src/types/config.model.ts +++ b/src/types/config.model.ts @@ -7,6 +7,7 @@ interface RedisConfig extends redis.RedisClientOptions { getTimeout: number; maxConnections: number; maxWriteConnections: number; + disableHashCache: boolean; } interface RedisReadOnlyConfig extends redis.RedisClientOptions { diff --git a/src/utils/getHashCache.ts b/src/utils/getHashCache.ts index 7ff0ef0..0c15cc0 100644 --- a/src/utils/getHashCache.ts +++ b/src/utils/getHashCache.ts @@ -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(value: T, times = defaulted async function getFromRedis(key: HashedValue): Promise { 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; } \ No newline at end of file