Files
web/packages/common/dataCache/DistributedCache/set.ts
Anton Gunnarsson e1ede52014 Merged in fix/common-package-fix (pull request #2355)
fix: Tiny fixes in common package

* Fix self-referencing and circular imports

* Fix env isServer


Approved-by: Joakim Jäderberg
2025-06-13 11:36:40 +00:00

33 lines
966 B
TypeScript

import * as Sentry from "@sentry/nextjs"
import { env } from "../../env/server"
import { safeTry } from "../../utils/safeTry"
import { type CacheTime, getCacheTimeInSeconds } from "../Cache"
import { getCacheEndpoint } from "./endpoints"
const API_KEY = env.REDIS_API_KEY ?? ""
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,
},
})
}
}