Merged in feature/warmup (pull request #1887)
* unified warmup function Approved-by: Linus Flood
This commit is contained in:
@@ -1,13 +1,18 @@
|
||||
import { type CacheTime, type DataCache } from "@/services/dataCache/Cache"
|
||||
import { cacheLogger } from "@/services/dataCache/logger"
|
||||
|
||||
import {
|
||||
type CacheOrGetOptions,
|
||||
shouldGetFromCache,
|
||||
} from "../../cacheOrGetOptions"
|
||||
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
|
||||
ttl: CacheTime,
|
||||
opts?: CacheOrGetOptions
|
||||
): Promise<T> => {
|
||||
if (Array.isArray(key)) {
|
||||
key = key.join("-")
|
||||
@@ -18,14 +23,16 @@ export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
|
||||
realTTL = cacheTime
|
||||
}
|
||||
|
||||
const cached = await get(key)
|
||||
let cached: Awaited<T> | undefined = undefined
|
||||
if (shouldGetFromCache(opts)) {
|
||||
cached = await get(key)
|
||||
if (cached) {
|
||||
return cached
|
||||
}
|
||||
|
||||
if (cached) {
|
||||
return cached as T
|
||||
cacheLogger.debug(`Miss for key '${key}'`)
|
||||
}
|
||||
|
||||
cacheLogger.debug(`Miss for key '${key}'`)
|
||||
|
||||
try {
|
||||
const data = await callback(overrideTTL)
|
||||
await set(key, data, realTTL)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { unstable_cache } from "next/cache"
|
||||
import { revalidateTag, unstable_cache } from "next/cache"
|
||||
|
||||
import {
|
||||
type CacheTime,
|
||||
@@ -6,18 +6,26 @@ import {
|
||||
getCacheTimeInSeconds,
|
||||
} from "@/services/dataCache/Cache"
|
||||
|
||||
import {
|
||||
type CacheOrGetOptions,
|
||||
shouldGetFromCache,
|
||||
} from "../../cacheOrGetOptions"
|
||||
import { cacheLogger } from "../../logger"
|
||||
|
||||
export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
|
||||
key: string | string[],
|
||||
callback: () => Promise<T>,
|
||||
ttl: CacheTime
|
||||
ttl: CacheTime,
|
||||
opts?: CacheOrGetOptions
|
||||
): Promise<T> => {
|
||||
if (!Array.isArray(key)) {
|
||||
key = [key]
|
||||
}
|
||||
|
||||
const perf = performance.now()
|
||||
if (!shouldGetFromCache(opts)) {
|
||||
revalidateTag(key[0])
|
||||
}
|
||||
|
||||
const res = await unstable_cache(callback, key, {
|
||||
revalidate: getCacheTimeInSeconds(ttl),
|
||||
|
||||
Reference in New Issue
Block a user