Skip to content

Commit

Permalink
undo cache in layout
Browse files Browse the repository at this point in the history
  • Loading branch information
ddxv committed Dec 12, 2024
1 parent fc2f7ef commit fc4c677
Showing 1 changed file with 12 additions and 52 deletions.
64 changes: 12 additions & 52 deletions frontend/src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,18 @@
import type { LayoutServerLoad } from './$types';

// Create a simple cache store
let cache = {
appCats: null,
appsOverview: null,
companyTypes: null,
lastFetched: 0
};

// Cache duration (e.g., 24 hours in milliseconds)
const CACHE_DURATION = 24 * 60 * 60 * 1000;

export const load: LayoutServerLoad = async ({ fetch }) => {
// Check if cache is valid
if (
cache.appCats &&
cache.appsOverview &&
cache.companyTypes &&
Date.now() - cache.lastFetched < CACHE_DURATION
) {
return {
appCats: cache.appCats,
appsOverview: cache.appsOverview,
companyTypes: cache.companyTypes
};
}

// If cache is invalid or empty, fetch new data
try {
const [appCats, appsOverview, companyTypes] = await Promise.all([
fetch(`http://localhost:8000/api/categories`).then((res) => res.json()),
fetch(`http://localhost:8000/api/apps/overview`).then((res) => res.json()),
fetch(`http://localhost:8000/api/companies/types`).then((res) => res.json())
]);
console.log(`root layout load appCats, appsOverview, companyTypes start`);
const [appCats, appsOverview, companyTypes] = await Promise.all([
fetch(`http://localhost:8000/api/categories`).then((res) => res.json()),
fetch(`http://localhost:8000/api/apps/overview`).then((res) => res.json()),
fetch(`http://localhost:8000/api/companies/types`).then((res) => res.json())
]);

// Update cache
cache = {
appCats,
appsOverview,
companyTypes,
lastFetched: Date.now()
};
console.log(`root layout load appCats, appsOverview, companyTypes end`);

return {
appCats,
appsOverview,
companyTypes
};
} catch (error) {
console.error('Error loading data:', error);
return {
appCats: [],
appsOverview: [],
companyTypes: []
};
}
return {
appCats: appCats,
appsOverview: appsOverview,
companyTypes: companyTypes
};
};

0 comments on commit fc4c677

Please sign in to comment.