From 73a45dcc1816bc11816e642564a658db179e9aa7 Mon Sep 17 00:00:00 2001 From: moris Date: Sun, 15 Dec 2024 22:37:42 +0900 Subject: [PATCH] =?UTF-8?q?add:=20=E3=82=AD=E3=83=A3=E3=83=83=E3=82=B7?= =?UTF-8?q?=E3=83=A5=E3=81=AB=E6=9C=89=E5=8A=B9=E6=9C=9F=E9=99=90=E3=82=92?= =?UTF-8?q?=E8=A8=AD=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/server/Metadatas.js | 39 +++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/lib/server/Metadatas.js b/src/lib/server/Metadatas.js index eb4055f..042df4e 100644 --- a/src/lib/server/Metadatas.js +++ b/src/lib/server/Metadatas.js @@ -5,6 +5,8 @@ import path from 'node:path'; import Posts from './PostList'; import Load from './LoadPost'; +import { dev } from '$app/environment'; + async function Metadatas() { const cache_dir = '/tmp/day.moris.blog/'; @@ -21,29 +23,46 @@ async function Metadatas() { const sorted = metadataList .sort((a,b)=>{ - return b.metadata.date.getTime() - a.metadata.date.getTime() - }) + return b.metadata.date.getTime() - a.metadata.date.getTime() + }) .filter((m)=> dev||m.metadata.published) return sorted } + async function makeCache() { + let data = await build() + let cache_data = { + cached_time: Date.now(), + dev, + data + } + + if (!fs.existsSync(cache_dir)) { + fs.mkdirSync(cache_dir), {recursive: true} + } + + fs.writeFileSync(cache_file, JSON.stringify(cache_data, null, 2), 'utf8') + return data + } + if (fs.existsSync(cache_file)) { - const metas = JSON.parse(fs.readFileSync(cache_file, 'utf8'), (key,value)=>{ + const cache = JSON.parse(fs.readFileSync(cache_file, 'utf8'), (key,value)=>{ if(key=='date'){ return (new Date(value)); } return value }) - return metas + if (cache.dev^dev || Date.now()-cache.cached_time>5*60000) { + const data = await makeCache() + return data + } else { + return cache.data + } } else { - const metas = await build() - - if(!fs.existsSync(cache_dir)){fs.mkdirSync(cache_dir), {recursive: true}} - fs.writeFileSync(cache_file, JSON.stringify(metas), 'utf8') - - return metas + const data = await makeCache() + return data } } \ No newline at end of file