Skip to content

Commit

Permalink
Merge pull request #2333 from Igloczek/feature/output-cache-clearing-…
Browse files Browse the repository at this point in the history
…versioning

Output cache clearing versioning
  • Loading branch information
pkarw committed Feb 2, 2019
2 parents 515d84c + 3c638d0 commit 2a1d3d1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 35 deletions.
13 changes: 1 addition & 12 deletions core/scripts/cache.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
const program = require('commander')
const config = require('config')
const TagCache = require('redis-tag-cache').default

let cache
if (config.server.useOutputCache) {
cache = new TagCache({
redis: config.redis,
defaultTimeout: config.server.outputCacheDefaultTtl // Expire records after a day (even if they weren't invalidated)
})
console.log('Redis cache set', config.redis)
} else {
console.error('Output cache is disabled in the config')
}
const cache = require('./utils/cache-instance')

program
.command('clear')
Expand Down
31 changes: 10 additions & 21 deletions core/scripts/server.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const fs = require('fs')
const path = require('path')
const express = require('express')
const compile = require('lodash.template')
const rootPath = require('app-root-path').path
const resolve = file => path.resolve(rootPath, file)

const cache = require('./utils/cache-instance')
const apiStatus = require('./utils/api-status')

let config = require('config')
const TagCache = require('redis-tag-cache').default
const utils = require('./server/utils')
const compile = require('lodash.template')

const compileOptions = {
escape: /{{([^{][\s\S]+?[^}])}}/g,
Expand All @@ -18,19 +20,6 @@ process.noDeprecation = true

const app = express()

let cache
if (config.server.useOutputCache) {
const cacheKey = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'build', 'cache-version.json')) || '')
const redisConfig = Object.assign(config.redis, { keyPrefix: cacheKey })

cache = new TagCache({
redis: redisConfig,
defaultTimeout: config.server.outputCacheDefaultTtl
})

console.log('Redis cache set', redisConfig)
}

const templatesCache = {}
let renderer
for (const tplName of Object.keys(config.ssr.templates)) {
Expand Down Expand Up @@ -76,7 +65,7 @@ function invalidateCache (req, res) {
if (req.query.tag && req.query.key) { // clear cache pages for specific query tag
if (req.query.key !== config.server.invalidateCacheKey) {
console.error('Invalid cache invalidation key')
utils.apiStatus(res, 'Invalid cache invalidation key', 500)
apiStatus(res, 'Invalid cache invalidation key', 500)
return
}
console.log(`Clear cache request for [${req.query.tag}]`)
Expand All @@ -99,17 +88,17 @@ function invalidateCache (req, res) {
}
})
Promise.all(subPromises).then(r => {
utils.apiStatus(res, `Tags invalidated successfully [${req.query.tag}]`, 200)
apiStatus(res, `Tags invalidated successfully [${req.query.tag}]`, 200)
}).catch(error => {
utils.apiStatus(res, error, 500)
apiStatus(res, error, 500)
console.error(error)
})
} else {
utils.apiStatus(res, 'Invalid parameters for Clear cache request', 500)
apiStatus(res, 'Invalid parameters for Clear cache request', 500)
console.error('Invalid parameters for Clear cache request')
}
} else {
utils.apiStatus(res, 'Cache invalidation is not required, output cache is disabled', 200)
apiStatus(res, 'Cache invalidation is not required, output cache is disabled', 200)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @param {number} [code=200] Status code to send on success
* @param {json} [result='OK'] Text message or result information object
*/
module.exports.apiStatus = (res, result = 'OK', code = 200, meta = null) => {
module.exports = (res, result = 'OK', code = 200, meta = null) => {
let apiResult = { code: code, result: result }
if (meta !== null) {
apiResult.meta = meta
Expand Down
21 changes: 21 additions & 0 deletions core/scripts/utils/cache-instance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const fs = require('fs')
const path = require('path')
const TagCache = require('redis-tag-cache').default
const config = require('config')

module.exports = () => {
if (config.server.useOutputCache) {
return false
}

const cacheVersionPath = path.join(__dirname, '..', 'build', 'cache-version.json')
const cacheKey = JSON.parse(fs.readFileSync(cacheVersionPath) || '')
const redisConfig = Object.assign(config.redis, { keyPrefix: cacheKey })

console.log('Redis cache set', redisConfig)

return new TagCache({
redis: redisConfig,
defaultTimeout: config.server.outputCacheDefaultTtl
})
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"isomorphic-fetch": "^2.2.1",
"js-sha3": "^0.8.0",
"localforage": "^1.7.2",
"lru-cache": "^4.0.1",
"magento2-rest-client": "github:DivanteLtd/magento2-rest-client",
"pm2": "^2.10.4",
"reflect-metadata": "^0.1.12",
Expand Down

0 comments on commit 2a1d3d1

Please sign in to comment.