Merged in feature/redis (pull request #1478)

Distributed cache

* cache deleteKey now uses an options object instead of a lonely argument variable fuzzy

* merge

* remove debug logs and cleanup

* cleanup

* add fault handling

* add fault handling

* add pid when logging redis client creation

* add identifier when logging redis client creation

* cleanup

* feat: add redis-api as it's own app

* feature: use http wrapper for redis

* feat: add the possibility to fallback to unstable_cache

* Add error handling if redis cache is unresponsive

* add logging for unstable_cache

* merge

* don't cache errors

* fix: metadatabase on branchdeploys

* Handle when /en/destinations throws
add ErrorBoundary

* Add sentry-logging when ErrorBoundary catches exception

* Fix error handling for distributed cache

* cleanup code

* Added Application Insights back

* Update generateApiKeys script and remove duplicate

* Merge branch 'feature/redis' of bitbucket.org:scandic-swap/web into feature/redis

* merge


Approved-by: Linus Flood
This commit is contained in:
Joakim Jäderberg
2025-03-14 07:54:21 +00:00
committed by Linus Flood
parent a8304e543e
commit fa63b20ed0
141 changed files with 4404 additions and 1941 deletions

View File

@@ -0,0 +1,9 @@
export const cacheMap = new Map<
string,
{
/** Absolute expiration timestamp (`Date.now()`) */
expiresAt: number
/** The cached data */
data: unknown
}
>()

View File

@@ -0,0 +1,36 @@
import { type CacheTime, type DataCache } from "@/services/dataCache/Cache"
import { cacheLogger } from "@/services/dataCache/logger"
import { get } from "./get"
import { set } from "./set"
export const cacheOrGet: DataCache["cacheOrGet"] = async <T>(
key: string | string[],
callback: () => Promise<T>,
ttl: CacheTime
): Promise<T> => {
if (Array.isArray(key)) {
key = key.join("-")
}
const cached = await get(key)
if (cached) {
return cached as T
}
cacheLogger.debug(`Miss for key '${key}'`)
try {
const data = await callback()
await set(key, data, ttl)
return data
} catch (e) {
cacheLogger.error(
`Error while fetching data for key '${key}', avoid caching`,
e
)
throw e
}
}

View File

@@ -0,0 +1,9 @@
import { cacheLogger } from "@/services/dataCache/logger"
import { cacheMap } from "./cacheMap"
export async function deleteAll() {
cacheLogger.debug("Deleting all keys")
cacheMap.clear()
}

View File

@@ -0,0 +1,17 @@
import { cacheLogger } from "@/services/dataCache/logger"
import { cacheMap } from "./cacheMap"
export async function deleteKey(key: string, opts?: { fuzzy?: boolean }) {
cacheLogger.debug("Deleting key", key)
if (opts?.fuzzy) {
cacheMap.forEach((_, k) => {
if (k.includes(key)) {
cacheMap.delete(k)
}
})
return
}
cacheMap.delete(key)
}

View File

@@ -0,0 +1,23 @@
import { cacheLogger } from "@/services/dataCache/logger"
import { cacheMap } from "./cacheMap"
export async function get<T>(key: string): Promise<T | undefined> {
const cached = cacheMap.get(key)
if (!cached) {
return undefined
}
if (cached.expiresAt < Date.now()) {
cacheLogger.debug(`Expired for key '${key}'`)
cacheMap.delete(key)
return undefined
}
if (cached.data === undefined) {
cacheLogger.debug(`Data is undefined for key '${key}'`)
cacheMap.delete(key)
return undefined
}
return cached.data as T
}

View File

@@ -0,0 +1,10 @@
import { cacheOrGet } from "./cacheOrGet"
import { deleteKey } from "./deleteKey"
import { get } from "./get"
import { set } from "./set"
import type { DataCache } from "@/services/dataCache/Cache"
export async function createInMemoryCache(): Promise<DataCache> {
return { type: "in-memory", cacheOrGet, deleteKey, get, set }
}

View File

@@ -0,0 +1,17 @@
import {
type CacheTime,
getCacheTimeInSeconds,
} from "@/services/dataCache/Cache"
import { cacheMap } from "./cacheMap"
export async function set<T>(
key: string,
data: T,
ttl: CacheTime
): Promise<void> {
cacheMap.set(key, {
data: data,
expiresAt: Date.now() + getCacheTimeInSeconds(ttl) * 1000,
})
}