diff --git a/apps/scandic-web/app/api/web/revalidate/manually/route.ts b/apps/scandic-web/app/api/web/revalidate/manually/route.ts index 5420d1f12..27c92acfd 100644 --- a/apps/scandic-web/app/api/web/revalidate/manually/route.ts +++ b/apps/scandic-web/app/api/web/revalidate/manually/route.ts @@ -49,7 +49,7 @@ export async function POST() { revalidateManuallyLogger.info(`Tag: ${tag}`) revalidateTag(tag) - cacheClient.deleteKey(tag, { fuzzy: false }) + cacheClient.deleteKey(tag, { fuzzy: false, includeGitHashInKey: true }) return Response.json({ revalidated: true, now: Date.now() }) } catch (error) { diff --git a/packages/common/dataCache/Cache.ts b/packages/common/dataCache/Cache.ts index 1b14a5fe9..2c68a0349 100644 --- a/packages/common/dataCache/Cache.ts +++ b/packages/common/dataCache/Cache.ts @@ -98,7 +98,10 @@ export type DataCache = { * @param fuzzy If true, does a wildcard delete. *key* * @returns */ - deleteKey: (key: string, opts?: { fuzzy?: boolean }) => Promise + deleteKey: ( + key: string, + opts?: { fuzzy?: boolean; includeGitHashInKey?: boolean } + ) => Promise /** * Deletes a key from the cache @@ -106,5 +109,8 @@ export type DataCache = { * @param fuzzy If true, does a wildcard delete. *key* * @returns */ - deleteKeys: (keys: string[], opts?: { fuzzy?: boolean }) => Promise + deleteKeys: ( + keys: string[], + opts?: { fuzzy?: boolean; includeGitHashInKey?: boolean } + ) => Promise } diff --git a/packages/common/dataCache/DistributedCache/deleteKey.ts b/packages/common/dataCache/DistributedCache/deleteKey.ts index 957a8b0b9..4ac91560e 100644 --- a/packages/common/dataCache/DistributedCache/deleteKey.ts +++ b/packages/common/dataCache/DistributedCache/deleteKey.ts @@ -4,11 +4,24 @@ import { env } from "../../env/server" import { safeTry } from "../../utils/safeTry" import { cacheLogger } from "../logger" import { getCacheEndpoint } from "./endpoints" +import { generateCacheKey } from "./generateCacheKey" const API_KEY = env.REDIS_API_KEY ?? "" -export async function deleteKey(key: string, opts?: { fuzzy?: boolean }) { +export async function deleteKey( + key: string, + opts?: { fuzzy?: boolean; includeGitHashInKey?: boolean } +) { const perf = performance.now() - const endpoint = getCacheEndpoint(key) + + let cacheKey: string | undefined = key + + if (opts?.includeGitHashInKey) { + cacheKey = generateCacheKey(key, { + includeGitHashInKey: true, + }) + } + + const endpoint = getCacheEndpoint(cacheKey) if (opts?.fuzzy) { endpoint.searchParams.set("fuzzy", "true") @@ -29,7 +42,7 @@ export async function deleteKey(key: string, opts?: { fuzzy?: boolean }) { if (response?.status !== 404) { Sentry.captureException(error ?? new Error("Unable to DELETE cachekey"), { extra: { - cacheKey: key, + cacheKey: cacheKey, statusCode: response?.status, statusText: response?.statusText, }, diff --git a/packages/common/dataCache/DistributedCache/deleteKeys.ts b/packages/common/dataCache/DistributedCache/deleteKeys.ts index 21fc15fca..108b16e3b 100644 --- a/packages/common/dataCache/DistributedCache/deleteKeys.ts +++ b/packages/common/dataCache/DistributedCache/deleteKeys.ts @@ -4,12 +4,25 @@ import { env } from "../../env/server" import { safeTry } from "../../utils/safeTry" import { cacheLogger } from "../logger" import { getDeleteMultipleKeysEndpoint } from "./endpoints" +import { generateCacheKey } from "./generateCacheKey" const API_KEY = env.REDIS_API_KEY ?? "" -export async function deleteKeys(keys: string[], opts?: { fuzzy?: boolean }) { +export async function deleteKeys( + keys: string[], + opts?: { fuzzy?: boolean; includeGitHashInKey?: boolean } +) { const perf = performance.now() const endpoint = getDeleteMultipleKeysEndpoint() + let cacheKeys: string[] = keys + if (opts?.includeGitHashInKey) { + cacheKeys = keys.map((key) => + generateCacheKey(key, { + includeGitHashInKey: true, + }) + ) + } + const [response, error] = await safeTry( fetch(endpoint, { method: "DELETE", @@ -18,7 +31,7 @@ export async function deleteKeys(keys: string[], opts?: { fuzzy?: boolean }) { "Content-Type": "application/json", "x-api-key": API_KEY, }, - body: JSON.stringify({ keys, fuzzy: opts?.fuzzy ?? false }), + body: JSON.stringify({ keys: cacheKeys, fuzzy: opts?.fuzzy ?? false }), signal: AbortSignal.timeout(10_000), }) )