Skip to content

Commit

Permalink
Add a in-memory page cache for renderings (github#16173)
Browse files Browse the repository at this point in the history
* 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 <chiedo@users.noreply.github.com>
  • Loading branch information
chiedo and chiedo committed Oct 22, 2020
1 parent 6a532d7 commit 73559d9
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion middleware/render-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}

0 comments on commit 73559d9

Please sign in to comment.