From 29d632c1514d6edacdfebe6deae4c95fc5a0f621 Mon Sep 17 00:00:00 2001 From: Elias Schneider Date: Wed, 23 Oct 2024 11:48:46 +0200 Subject: [PATCH] fix: cache version information for 3 hours --- .../lib/types/application-configuration.ts | 6 +++++ .../src/routes/settings/+layout.server.ts | 24 +++++++++++++++++++ frontend/src/routes/settings/+layout.ts | 11 --------- 3 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 frontend/src/routes/settings/+layout.server.ts delete mode 100644 frontend/src/routes/settings/+layout.ts diff --git a/frontend/src/lib/types/application-configuration.ts b/frontend/src/lib/types/application-configuration.ts index ec4edd6..af5e59e 100644 --- a/frontend/src/lib/types/application-configuration.ts +++ b/frontend/src/lib/types/application-configuration.ts @@ -16,3 +16,9 @@ export type AppConfigRawResponse = { type: string; value: string; }[]; + +export type AppVersionInformation = { + isUpToDate: boolean; + newestVersion: string; + currentVersion: string; +}; \ No newline at end of file diff --git a/frontend/src/routes/settings/+layout.server.ts b/frontend/src/routes/settings/+layout.server.ts new file mode 100644 index 0000000..2203fdc --- /dev/null +++ b/frontend/src/routes/settings/+layout.server.ts @@ -0,0 +1,24 @@ +import AppConfigService from '$lib/services/app-config-service'; +import type { AppVersionInformation } from '$lib/types/application-configuration'; +import type { LayoutServerLoad } from './$types'; + +let versionInformation: AppVersionInformation; +let versionInformationLastUpdated: number; + +export const load: LayoutServerLoad = async () => { + const appConfigService = new AppConfigService(); + + // Cache the version information for 3 hours + const cacheExpired = + versionInformationLastUpdated && + Date.now() - versionInformationLastUpdated > 1000 * 60 * 60 * 3; + + if (!versionInformation || cacheExpired) { + versionInformation = await appConfigService.getVersionInformation(); + versionInformationLastUpdated = Date.now(); + } + + return { + versionInformation + }; +}; diff --git a/frontend/src/routes/settings/+layout.ts b/frontend/src/routes/settings/+layout.ts deleted file mode 100644 index 0cd6c6d..0000000 --- a/frontend/src/routes/settings/+layout.ts +++ /dev/null @@ -1,11 +0,0 @@ -import AppConfigService from '$lib/services/app-config-service'; -import type { LayoutLoad } from './$types'; - -export const load: LayoutLoad = async () => { - const appConfigService = new AppConfigService(); - - const versionInformation = await appConfigService.getVersionInformation(); - return { - versionInformation - }; -};