Merged in feat/sw-3596-cache (pull request #3106)

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
This commit is contained in:
Linus Flood
2025-11-11 06:52:47 +00:00
parent 0b569e28ce
commit 1b35618eb2
3 changed files with 30 additions and 11 deletions

View File

@@ -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 <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,
})
@@ -24,12 +34,6 @@ export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
cachedValue = await get<T>(cacheKey)
}
let realTTL = ttl
const overrideTTL = function (cacheTime: CacheTime) {
realTTL = cacheTime
}
if (!cachedValue) {
const perf = performance.now()
const data = await callback(overrideTTL)