From 991c5313c5aff5a3f9f2ba828355e9928c710308 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 23 Jun 2022 02:54:42 +0200 Subject: [PATCH] cache readme data --- src/routes/index.js | 2 +- src/utils/readme.js | 27 +++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/routes/index.js b/src/routes/index.js index 240e5a5..d4fbba4 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -5,7 +5,7 @@ const readme = require('../utils/readme') // Home Page router.get('/', (_, res) => { - readme.data() + readme.get() .then(data => res.render('index', { readme: marked.parse(data) })) }) diff --git a/src/utils/readme.js b/src/utils/readme.js index 05b9917..2acfb03 100644 --- a/src/utils/readme.js +++ b/src/utils/readme.js @@ -1,10 +1,29 @@ const fetch = require('node-fetch') -async function data () { - return await fetch('https://git.kennel.ml/Anri/Constnium/raw/branch/main/README.md') - .then(res => res.text()) +// URL of the repo +const repoURL = 'https://git.kennel.ml/Anri/Constnium/raw/branch/main/README.md' + +// Store the data from the repo +let data = null + +// Store last time data as been pulled from the repo +let timestamp = 0 + +// Retrieve the data from the repo +async function get () { + const now = Date.now() + // If data is older than one day, refresh it + if (Math.ceil(Math.abs(now - timestamp) / (1000 * 3600 * 24)) > 2 || data === null) { + await fetch(repoURL) + .then(res => { + data = res.text() + timestamp = now + }) + } + + return data } module.exports = { - data + get }