Files
web/apps/scandic-web/services/dataCache/DistributedCache/set.ts
Joakim Jäderberg b977877e93 Merged in fix/limit-time-to-response-for-cache-calls (pull request #1990)
fix: add timeout for redis-api calls SW-2635

Approved-by: Michael Zetterberg
2025-05-07 09:15:58 +00:00

33 lines
919 B
TypeScript

import * as Sentry from "@sentry/nextjs"
import { safeTry } from "@/utils/safeTry"
import { type CacheTime, getCacheTimeInSeconds } from "../Cache"
import { API_KEY } from "./client"
import { getCacheEndpoint } from "./endpoints"
export async function set<T>(key: string, value: T, ttl: CacheTime) {
const [response, error] = await safeTry(
fetch(getCacheEndpoint(key), {
method: "PUT",
headers: {
"Content-Type": "application/json",
"x-api-key": API_KEY,
},
body: JSON.stringify({ data: value, ttl: getCacheTimeInSeconds(ttl) }),
cache: "no-cache",
signal: AbortSignal.timeout(3_000),
})
)
if (!response || !response.ok || error) {
Sentry.captureException(error ?? new Error("Unable to SET cachekey"), {
extra: {
cacheKey: key,
statusCode: response?.status,
statusText: response?.statusText,
},
})
}
}