feat(SW-3598): Don't call redis api if cachetime=0 * feat(SW-3596): no cache if ttl=0 * Fixed on set as well * No cache if redemption * Revert no cache if redemption Approved-by: Joakim Jäderberg
55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
import { type CacheTime, type DataCache, getCacheTimeInSeconds } from "../Cache"
|
|
import {
|
|
type CacheOrGetOptions,
|
|
shouldGetFromCache,
|
|
} from "../cacheOrGetOptions"
|
|
import { cacheLogger } from "../logger"
|
|
import { generateCacheKey } from "./generateCacheKey"
|
|
import { get } from "./get"
|
|
import { set } from "./set"
|
|
|
|
export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
|
|
key: string | string[],
|
|
callback: (overrideTTL: (cacheTime: CacheTime) => void) => Promise<T>,
|
|
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,
|
|
})
|
|
|
|
let cachedValue: Awaited<T> | undefined = undefined
|
|
if (shouldGetFromCache(opts)) {
|
|
cachedValue = await get<T>(cacheKey)
|
|
}
|
|
|
|
if (!cachedValue) {
|
|
const perf = performance.now()
|
|
const data = await callback(overrideTTL)
|
|
|
|
const size = JSON.stringify(data).length / (1024 * 1024)
|
|
if (size >= 5) {
|
|
cacheLogger.warn(`'${key}' is larger than 5MB!`)
|
|
}
|
|
cacheLogger.debug(
|
|
`Fetching data took ${(performance.now() - perf).toFixed(2)}ms ${size.toFixed(4)}MB for '${key}'`
|
|
)
|
|
|
|
await set<T>(cacheKey, data, realTTL)
|
|
return data
|
|
}
|
|
|
|
return cachedValue
|
|
}
|