Skip to content
Extraits de code Groupes Projets
Valider e08c47c8 rédigé par Steel's avatar Steel
Parcourir les fichiers

better caching

parent d87e118b
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
<html lang="fr" class="h-full"> <html lang="fr" class="h-full">
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" /> <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
......
...@@ -12,7 +12,8 @@ const cache = new Map<number, PromoCache>(); ...@@ -12,7 +12,8 @@ const cache = new Map<number, PromoCache>();
// 1 hour // 1 hour
const CACHE_DURATION_MS = 1000 * 60 * 60; const CACHE_DURATION_MS = 1000 * 60 * 60;
async function cacheImages(users: UserId[]): Promise<void> { async function cacheImages(users: Iterable<UserId>): Promise<void> {
let count = 0;
for (const user of users) { for (const user of users) {
const response = await client.query(USER_DETAILS_QUERY, { idList: [user] }); const response = await client.query(USER_DETAILS_QUERY, { idList: [user] });
...@@ -23,17 +24,22 @@ async function cacheImages(users: UserId[]): Promise<void> { ...@@ -23,17 +24,22 @@ async function cacheImages(users: UserId[]): Promise<void> {
const photo = response.data?.page.nodes[0].photo; const photo = response.data?.page.nodes[0].photo;
if (!photo?.url) continue; if (!photo?.url) continue;
await fetch(photo.url, { method: "HEAD" }); await fetch(photo.url, { method: "HEAD" });
count++;
} }
console.log("cached", users.length, "images"); console.log("cached", count, "images");
}
// background task
async function cachePromotionImages(year: number) {
const promo = cache.get(year);
if (!promo) return;
await cacheImages(promo.promotion);
} }
async function fetchPromotion(year: number): Promise<PromoCache> { async function fetchPromotion(year: number): Promise<PromoCache> {
const array = await Array.fromAsync(pageIterator(PROMOTION_QUERY, { year })); const array = await Array.fromAsync(pageIterator(PROMOTION_QUERY, { year }));
const users = array.map((node) => node.id); const users = array.map((node) => node.id);
// background task
cacheImages(users);
return { return {
lastUpdateMs: new Date().getTime(), lastUpdateMs: new Date().getTime(),
promotion: new Set(users), promotion: new Set(users),
...@@ -49,6 +55,7 @@ export async function getPromotion(year: number): Promise<Set<UserId>> { ...@@ -49,6 +55,7 @@ export async function getPromotion(year: number): Promise<Set<UserId>> {
console.log("year", year, "not in cache, fetching"); console.log("year", year, "not in cache, fetching");
const freshData = await fetchPromotion(year); const freshData = await fetchPromotion(year);
cache.set(year, freshData); cache.set(year, freshData);
cachePromotionImages(year);
return freshData.promotion; return freshData.promotion;
} }
...@@ -70,3 +77,9 @@ export function getPromotionRange( ...@@ -70,3 +77,9 @@ export function getPromotionRange(
(array) => new Set(array), (array) => new Set(array),
); );
} }
setInterval(() => {
for (const year of cache.keys()) {
cachePromotionImages(year);
}
}, CACHE_DURATION_MS / 2);
...@@ -66,7 +66,7 @@ export async function load(event) { ...@@ -66,7 +66,7 @@ export async function load(event) {
} }
const MAX_POINTS = 10; const MAX_POINTS = 10;
const MIN_POINTS = 0; const MIN_POINTS = 3;
// Durée maximale pour obtenir le score maximal // Durée maximale pour obtenir le score maximal
const SERVER_DELAY_MAX = 3; const SERVER_DELAY_MAX = 3;
......
static/favicon.png

1,53 ko

0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter