Files
web/apps/scandic-web/services/dataCache/Cache.ts
Joakim Jäderberg fa63b20ed0 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
2025-03-14 07:54:21 +00:00

97 lines
2.6 KiB
TypeScript

const ONE_HOUR_IN_SECONDS = 3_600 as const
const ONE_DAY_IN_SECONDS = 86_400 as const
export const namedCacheTimeMap: Record<NamedCacheTimes, number> = {
"no cache": 0,
"1m": 60,
"5m": 300,
"10m": 600,
"1h": ONE_HOUR_IN_SECONDS,
"3h": ONE_HOUR_IN_SECONDS * 3,
"6h": ONE_HOUR_IN_SECONDS * 6,
"1d": ONE_DAY_IN_SECONDS,
"3d": ONE_DAY_IN_SECONDS * 3,
max: ONE_DAY_IN_SECONDS * 30,
} as const
export const namedCacheTimes = [
"no cache",
"1m",
"5m",
"10m",
"1h",
"3h",
"6h",
"1d",
"3d",
"max",
] as const
export type NamedCacheTimes = (typeof namedCacheTimes)[number]
/**
* Retrieves the cache time in seconds based on the given cache time.
* @param cacheTime - The time value to determine, either a named cache time or a number of seconds.
* @returns The cache time in seconds.
*/
export const getCacheTimeInSeconds = (cacheTime: CacheTime): number => {
if (typeof cacheTime === "number") {
if (cacheTime < 0 || !Number.isInteger(cacheTime)) {
return 0
}
return cacheTime
}
return namedCacheTimeMap[cacheTime] ?? 0
}
export type CacheTime = NamedCacheTimes | number
export type DataCache = {
/**
* Type of cache
*/
type: "edge" | "redis" | "in-memory" | "unstable-cache"
/**
* Helper function that retrieves from the cache if it exists, otherwise calls the callback and caches the result.
* If the call fails, the cache is not updated.
* @param key The cache key
* @param getDataFromSource An async function that provides a value to cache
* @param ttl Time to live, either a named cache time or a number of seconds
* @returns The cached value or the result from the callback
*/
cacheOrGet: <T>(
key: string | string[],
getDataFromSource: () => Promise<T>,
ttl: CacheTime
) => Promise<T>
/**
* Get a value from the cache, if it exists
* @see `cacheOrGet` for a more convenient way to cache values
* @param key The cache key to retrieve the value for
* @returns The cached value or undefined if not found
*/
get: <T>(key: string) => Promise<T | undefined>
/**
* Sets a value in the cache.
* @see `cacheOrGet` for a more convenient way to cache values
* @param key CacheKey to set
* @param obj Value to be cached
* @param ttl Time to live, either a named cache time or a number of seconds
* @returns A promise that resolves when the value has been cached
*/
set: <T>(key: string, obj: T, ttl: CacheTime) => Promise<void>
/**
* Deletes a key from the cache
* @param key CacheKey to delete
* @param fuzzy If true, does a wildcard delete. *key*
* @returns
*/
deleteKey: (key: string, opts?: { fuzzy?: boolean }) => Promise<void>
}