diff --git a/packages/common/dataCache/DistributedCache/cacheOrGet.ts b/packages/common/dataCache/DistributedCache/cacheOrGet.ts index 64b9b9c48..fda5cc882 100644 --- a/packages/common/dataCache/DistributedCache/cacheOrGet.ts +++ b/packages/common/dataCache/DistributedCache/cacheOrGet.ts @@ -1,3 +1,4 @@ +import { type CacheTime, type DataCache, getCacheTimeInSeconds } from "../Cache" import { type CacheOrGetOptions, shouldGetFromCache, @@ -7,14 +8,23 @@ import { generateCacheKey } from "./generateCacheKey" import { get } from "./get" import { set } from "./set" -import type { CacheTime, DataCache } from "../Cache" - export const cacheOrGet: DataCache["cacheOrGet"] = async ( key: string | string[], callback: (overrideTTL: (cacheTime: CacheTime) => void) => Promise, ttl: CacheTime, opts?: CacheOrGetOptions ) => { + let realTTL = ttl + + const overrideTTL = function (cacheTime: CacheTime) { + realTTL = cacheTime + } + + if (getCacheTimeInSeconds(ttl) <= 0) { + cacheLogger.debug(`'Fetching ${key}' with ttl=0. Skipping cache!`) + return await callback(overrideTTL) + } + const cacheKey = generateCacheKey(key, { includeGitHashInKey: opts?.includeGitHashInKey ?? true, }) @@ -24,12 +34,6 @@ export const cacheOrGet: DataCache["cacheOrGet"] = async ( cachedValue = await get(cacheKey) } - let realTTL = ttl - - const overrideTTL = function (cacheTime: CacheTime) { - realTTL = cacheTime - } - if (!cachedValue) { const perf = performance.now() const data = await callback(overrideTTL) diff --git a/packages/common/dataCache/MemoryCache/InMemoryCache/cacheOrGet.ts b/packages/common/dataCache/MemoryCache/InMemoryCache/cacheOrGet.ts index cfac52029..15e3ad794 100644 --- a/packages/common/dataCache/MemoryCache/InMemoryCache/cacheOrGet.ts +++ b/packages/common/dataCache/MemoryCache/InMemoryCache/cacheOrGet.ts @@ -1,3 +1,8 @@ +import { + type CacheTime, + type DataCache, + getCacheTimeInSeconds, +} from "../../Cache" import { type CacheOrGetOptions, shouldGetFromCache, @@ -6,8 +11,6 @@ import { cacheLogger } from "../../logger" import { get } from "./get" import { set } from "./set" -import type { CacheTime, DataCache } from "../../Cache" - export const cacheOrGet: DataCache["cacheOrGet"] = async ( key: string | string[], callback: (overrideTTL?: (cacheTime: CacheTime) => void) => Promise, @@ -23,6 +26,11 @@ export const cacheOrGet: DataCache["cacheOrGet"] = async ( realTTL = cacheTime } + if (getCacheTimeInSeconds(ttl) <= 0) { + cacheLogger.debug(`'Fetching ${key}' with ttl=0. Skipping cache!`) + return await callback(overrideTTL) + } + let cached: Awaited | undefined = undefined if (shouldGetFromCache(opts)) { cached = await get(key) diff --git a/packages/common/dataCache/MemoryCache/InMemoryCache/set.ts b/packages/common/dataCache/MemoryCache/InMemoryCache/set.ts index bc02eb17f..247cd693e 100644 --- a/packages/common/dataCache/MemoryCache/InMemoryCache/set.ts +++ b/packages/common/dataCache/MemoryCache/InMemoryCache/set.ts @@ -1,4 +1,5 @@ import { type CacheTime, getCacheTimeInSeconds } from "../../Cache" +import { cacheLogger } from "../../logger" import { cacheMap } from "./cacheMap" export async function set( @@ -6,8 +7,14 @@ export async function set( data: T, ttl: CacheTime ): Promise { + const cacheTimeInSeconds = getCacheTimeInSeconds(ttl) + if (cacheTimeInSeconds <= 0) { + cacheLogger.info(`'Trying to set ${key}' with ttl=0. Skipping cache!`) + return + } + cacheMap.set(key, { data: data, - expiresAt: Date.now() + getCacheTimeInSeconds(ttl) * 1000, + expiresAt: Date.now() + cacheTimeInSeconds * 1000, }) }