diff --git a/apps/scandic-web/services/cms/fetchAndCacheEntry.ts b/apps/scandic-web/services/cms/fetchAndCacheEntry.ts index a82bf00eb..2beb22d20 100644 --- a/apps/scandic-web/services/cms/fetchAndCacheEntry.ts +++ b/apps/scandic-web/services/cms/fetchAndCacheEntry.ts @@ -10,9 +10,13 @@ export const fetchAndCacheEntry = async (path: string, lang: Lang) => { return cache.cacheOrGet( cacheKey, - async () => { + async (overrideTTL) => { const { contentType, uid } = await resolveEntry(path, lang) + if (!contentType || !uid) { + overrideTTL?.("10m") + } + return { contentType, uid, diff --git a/apps/scandic-web/services/dataCache/Cache.ts b/apps/scandic-web/services/dataCache/Cache.ts index 8f164c65e..93df84b1a 100644 --- a/apps/scandic-web/services/dataCache/Cache.ts +++ b/apps/scandic-web/services/dataCache/Cache.ts @@ -64,7 +64,9 @@ export type DataCache = { */ cacheOrGet: ( key: string | string[], - getDataFromSource: () => Promise, + getDataFromSource: ( + overrideTTL?: (cacheTime: CacheTime) => void + ) => Promise, ttl: CacheTime ) => Promise diff --git a/apps/scandic-web/services/dataCache/DistributedCache/cacheOrGet.ts b/apps/scandic-web/services/dataCache/DistributedCache/cacheOrGet.ts index d0c24e5d3..dcab4d279 100644 --- a/apps/scandic-web/services/dataCache/DistributedCache/cacheOrGet.ts +++ b/apps/scandic-web/services/dataCache/DistributedCache/cacheOrGet.ts @@ -7,21 +7,27 @@ import { set } from "./set" export const cacheOrGet: DataCache["cacheOrGet"] = async ( key: string | string[], - callback: () => Promise, + callback: (overrideTTL: (cacheTime: CacheTime) => void) => Promise, ttl: CacheTime ) => { const cacheKey = generateCacheKey(key) const cachedValue = await get(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(cacheKey, data, ttl) + await set(cacheKey, data, realTTL) return data } diff --git a/apps/scandic-web/services/dataCache/DistributedCache/deleteKey.ts b/apps/scandic-web/services/dataCache/DistributedCache/deleteKey.ts index a5b0af783..30ca1fdbd 100644 --- a/apps/scandic-web/services/dataCache/DistributedCache/deleteKey.ts +++ b/apps/scandic-web/services/dataCache/DistributedCache/deleteKey.ts @@ -4,10 +4,15 @@ import { cacheLogger } from "../logger" import { API_KEY } from "./client" import { getCacheEndpoint } from "./endpoints" -export async function deleteKey(key: string) { +export async function deleteKey(key: string, opts?: { fuzzy?: boolean }) { const perf = performance.now() + const endpoint = getCacheEndpoint(key) - const response = await fetch(getCacheEndpoint(key), { + if (opts?.fuzzy) { + endpoint.searchParams.set("fuzzy", "true") + } + + const response = await fetch(endpoint, { method: "DELETE", cache: "no-cache", headers: { diff --git a/apps/scandic-web/services/dataCache/MemoryCache/InMemoryCache/cacheOrGet.ts b/apps/scandic-web/services/dataCache/MemoryCache/InMemoryCache/cacheOrGet.ts index 428080605..6a9ff00b8 100644 --- a/apps/scandic-web/services/dataCache/MemoryCache/InMemoryCache/cacheOrGet.ts +++ b/apps/scandic-web/services/dataCache/MemoryCache/InMemoryCache/cacheOrGet.ts @@ -6,13 +6,18 @@ import { set } from "./set" export const cacheOrGet: DataCache["cacheOrGet"] = async ( key: string | string[], - callback: () => Promise, + callback: (overrideTTL?: (cacheTime: CacheTime) => void) => Promise, ttl: CacheTime ): Promise => { if (Array.isArray(key)) { key = key.join("-") } + let realTTL = ttl + const overrideTTL = function (cacheTime: CacheTime) { + realTTL = cacheTime + } + const cached = await get(key) if (cached) { @@ -22,8 +27,8 @@ export const cacheOrGet: DataCache["cacheOrGet"] = async ( cacheLogger.debug(`Miss for key '${key}'`) try { - const data = await callback() - await set(key, data, ttl) + const data = await callback(overrideTTL) + await set(key, data, realTTL) return data } catch (e) { diff --git a/apps/scandic-web/services/dataCache/MemoryCache/UnstableCache/cacheOrGet.ts b/apps/scandic-web/services/dataCache/MemoryCache/UnstableCache/cacheOrGet.ts index 04943fe45..e0d970c24 100644 --- a/apps/scandic-web/services/dataCache/MemoryCache/UnstableCache/cacheOrGet.ts +++ b/apps/scandic-web/services/dataCache/MemoryCache/UnstableCache/cacheOrGet.ts @@ -10,7 +10,7 @@ import { cacheLogger } from "../../logger" export const cacheOrGet: DataCache["cacheOrGet"] = async ( key: string | string[], - callback: () => Promise, + callback: (overrideTTL?: (cacheTime: CacheTime) => void) => Promise, ttl: CacheTime ): Promise => { if (!Array.isArray(key)) {