From 73559d914eee30e9819986b8fff5b62eb720d515 Mon Sep 17 00:00:00 2001 From: Chiedo John <2156688+chiedo@users.noreply.github.com> Date: Thu, 22 Oct 2020 14:35:19 -0400 Subject: [PATCH] Add a in-memory page cache for renderings (#16173) * Add a in-memory page cache for renderings * Update render-page.js * Update render-page.js * Update render-page.js * Make the render page cache more intelligent Co-authored-by: Chiedo --- middleware/render-page.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/middleware/render-page.js b/middleware/render-page.js index cd0c84965f0c..565ecf545189 100644 --- a/middleware/render-page.js +++ b/middleware/render-page.js @@ -6,8 +6,19 @@ const layouts = require('../lib/layouts') const getMiniTocItems = require('../lib/get-mini-toc-items') const Page = require('../lib/page') +// We've got lots of memory, let's use it +// We can eventually throw this into redis +const pageCache = {} + module.exports = async function renderPage (req, res, next) { const page = req.context.page + const originalUrl = req.originalUrl + + // Serve from the cache if possible + if (req.method === 'GET' && pageCache[originalUrl]) { + console.log(`Serving from cached version of ${originalUrl}`) + return res.send(pageCache[originalUrl]) + } // render a 404 page if (!page) { @@ -74,5 +85,11 @@ module.exports = async function renderPage (req, res, next) { const output = await liquid.parseAndRender(layout, context) - res.send(output) + // Save output to cache for the next time around + if (req.method === 'GET') { + pageCache[originalUrl] = output + } + + // send response + return res.send(output) }