Files
web/apps/scandic-web/services/dataCache/MemoryCache/UnstableCache/cacheOrGet.ts
Linus Flood dd3fed9423 Merged in fix/cache-service-token (pull request #1571)
fix(servicetoken): cache it when using unstable_cache

* fix(servicetoken): cache it when using unstable_cache

* Refactor and using cacheOrGet as get/set

* Refactor

* Use expiresAt from cached token


Approved-by: Anton Gunnarsson
2025-03-19 09:16:11 +00:00

30 lines
648 B
TypeScript

import { unstable_cache } from "next/cache"
import {
type CacheTime,
type DataCache,
getCacheTimeInSeconds,
} from "@/services/dataCache/Cache"
import { cacheLogger } from "../../logger"
export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
key: string | string[],
callback: () => Promise<T>,
ttl: CacheTime
): Promise<T> => {
if (!Array.isArray(key)) {
key = [key]
}
const perf = performance.now()
const res = await unstable_cache(callback, key, {
revalidate: getCacheTimeInSeconds(ttl),
tags: key,
})()
cacheLogger.debug(`'${key}' took ${(performance.now() - perf).toFixed(2)}ms`)
return res
}