Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/src/api/controllers/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { MonkeyRequest } from "../types";
export async function getConfiguration(
_req: MonkeyRequest,
): Promise<GetConfigurationResponse> {
const currentConfiguration = await Configuration.getLiveConfiguration();
const currentConfiguration = await Configuration.getCachedConfiguration(true);
return new MonkeyResponse("Configuration retrieved", currentConfiguration);
}

Expand Down
10 changes: 8 additions & 2 deletions backend/src/api/controllers/psa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { loadingCache } from "../../utils/loadingCache";

//cache for one minute
const cache = loadingCache<PSA[]>(1 * 60 * 1000, async () => {
return replaceObjectIds(await PsaDAL.get());
});

export async function getPsas(_req: MonkeyRequest): Promise<GetPsaResponse> {
const data = await PsaDAL.get();
return new MonkeyResponse("PSAs retrieved", replaceObjectIds(data));
return new MonkeyResponse("PSAs retrieved", (await cache()) ?? []);
}
27 changes: 27 additions & 0 deletions backend/src/utils/loadingCache.ts
Original file line number Diff line number Diff line change
@@ -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<T>} fn - A function that returns a promise resolving to the data to cache.
*
* @returns {() => Promise<T | undefined>}
*/
export function loadingCache<T>(
ttlMs: number,
fn: () => Promise<T>,
): () => Promise<T | undefined> {
let lastFetchTime = 0;
let cache: T | undefined;

return async () => {
if (lastFetchTime < Date.now() - ttlMs) {
lastFetchTime = Date.now();
cache = await fn();
}
return cache;
};
}