feat: better cache for versions

This commit is contained in:
diced
2025-06-11 20:21:52 -07:00
parent 438b9b5a67
commit 4b1ca07510
2 changed files with 15 additions and 3 deletions

View File

@@ -7,6 +7,7 @@ import fastifyPlugin from 'fastify-plugin';
export type ApiVersionResponse = {
details: ReturnType<typeof getVersion>;
data: VersionAPI;
cached: true;
};
interface VersionAPI {
@@ -31,6 +32,9 @@ interface VersionAPI {
const logger = log('api').c('version');
let cachedData: VersionAPI | null = null;
let cachedAt = 0;
export const PATH = '/api/version';
export default fastifyPlugin(
(server, _, done) => {
@@ -39,6 +43,11 @@ export default fastifyPlugin(
const details = getVersion();
// 6 hrs cache
if (cachedData && Date.now() - cachedAt < (6 * 60 * 60 * 1000)) {
return res.send({ data: cachedData, details, cached: true });
}
const url = new URL(config.features.versionAPI);
url.pathname = '/';
url.searchParams.set('details', JSON.stringify(details));
@@ -52,9 +61,13 @@ export default fastifyPlugin(
const data: VersionAPI = await resp.json();
cachedData = data;
cachedAt = Date.now();
return res.send({
data,
details,
cached: false,
});
} catch (e) {
logger.error('failed to fetch version details').error(e as Error);