Merged in fix/cache-fixes (pull request #1555)
fix/cache: reduce cachetime when null response from CS. Fix fuzzy delete * fix/cache: reduce cachetime when null response from CS. Fix fuzzy delete Approved-by: Anton Gunnarsson
This commit is contained in:
@@ -7,21 +7,27 @@ import { set } from "./set"
|
||||
|
||||
export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
|
||||
key: string | string[],
|
||||
callback: () => Promise<T>,
|
||||
callback: (overrideTTL: (cacheTime: CacheTime) => void) => Promise<T>,
|
||||
ttl: CacheTime
|
||||
) => {
|
||||
const cacheKey = generateCacheKey(key)
|
||||
const 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()
|
||||
const data = await callback(overrideTTL)
|
||||
|
||||
cacheLogger.debug(
|
||||
`Getting data '${cacheKey}' took ${(performance.now() - perf).toFixed(2)}ms`
|
||||
)
|
||||
|
||||
await set<T>(cacheKey, data, ttl)
|
||||
await set<T>(cacheKey, data, realTTL)
|
||||
return data
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user