diff --git a/backend/src/api/controllers/configuration.ts b/backend/src/api/controllers/configuration.ts index 9a1dbd528017..e833bb879e36 100644 --- a/backend/src/api/controllers/configuration.ts +++ b/backend/src/api/controllers/configuration.ts @@ -12,7 +12,7 @@ import { MonkeyRequest } from "../types"; export async function getConfiguration( _req: MonkeyRequest, ): Promise { - const currentConfiguration = await Configuration.getLiveConfiguration(); + const currentConfiguration = await Configuration.getCachedConfiguration(true); return new MonkeyResponse("Configuration retrieved", currentConfiguration); } diff --git a/backend/src/api/controllers/psa.ts b/backend/src/api/controllers/psa.ts index c5f561807ff7..eabbae7c385b 100644 --- a/backend/src/api/controllers/psa.ts +++ b/backend/src/api/controllers/psa.ts @@ -3,8 +3,14 @@ import * as PsaDAL from "../../dal/psa"; import { MonkeyResponse } from "../../utils/monkey-response"; import { replaceObjectIds } from "../../utils/misc"; import { MonkeyRequest } from "../types"; +import { PSA } from "@monkeytype/schemas/psas"; +import { cacheWithTTL } from "../../utils/ttl-cache"; + +//cache for one minute +const cache = cacheWithTTL(1 * 60 * 1000, async () => { + return replaceObjectIds(await PsaDAL.get()); +}); export async function getPsas(_req: MonkeyRequest): Promise { - const data = await PsaDAL.get(); - return new MonkeyResponse("PSAs retrieved", replaceObjectIds(data)); + return new MonkeyResponse("PSAs retrieved", (await cache()) ?? []); } diff --git a/backend/src/utils/ttl-cache.ts b/backend/src/utils/ttl-cache.ts new file mode 100644 index 000000000000..f7538be577b8 --- /dev/null +++ b/backend/src/utils/ttl-cache.ts @@ -0,0 +1,27 @@ +/** + * Creates a caching function that loads data with a specified TTL (Time-to-Live). + * If the cache has expired (based on TTL), it will re-fetch the data by calling the provided function. + * Otherwise, it returns the cached value. + * + * @template T - The type of the value being cached. + * + * @param {number} ttlMs - The Time-to-Live (TTL) in milliseconds. The cache will refetch on call after this duration. + * @param {() => Promise} fn - A function that returns a promise resolving to the data to cache. + * + * @returns {() => Promise} + */ +export function cacheWithTTL( + ttlMs: number, + fn: () => Promise, +): () => Promise { + let lastFetchTime = 0; + let cache: T | undefined; + + return async () => { + if (lastFetchTime < Date.now() - ttlMs) { + lastFetchTime = Date.now(); + cache = await fn(); + } + return cache; + }; +}