Skip to content

Commit

Permalink
Merge pull request #2359 from DivanteLtd/feature/cache-instance-fix
Browse files Browse the repository at this point in the history
Cache instance fix
  • Loading branch information
patzick committed Feb 4, 2019
2 parents b4bd15e + f843fdf commit d09b690
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `store/lib/search` has been moved to `core/lib/search` (#2225)
- `store/lib/multistore` has been moved to `core/lib/multistore` (#2224)
- After checkout create logged-in cart for logged-in users if using order Direct Backend Sync - @grimasod (#2302)
- Output cache clearing supports versioning - @igloczek (#2333 + #2359)

## [1.7.3] - 2019.01.31
### Fixed
Expand Down
15 changes: 7 additions & 8 deletions core/scripts/cache.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { Logger } from '@vue-storefront/core/lib/logger'

const program = require('commander')
const config = require('config')
const cache = require('./utils/cache-instance')

program
.command('clear')
.option('-t|--tag <tag>', 'tag name, available tags: ' + config.server.availableCacheTags.join(', '), '*')
.action((cmd) => { // TODO: add parallel processing
if (!cmd.tag) {
Logger.error('error: tag must be specified')()
console.error('error: tag must be specified')
process.exit(1)
} else {
Logger.log(`Clear cache request for [${cmd.tag}]`)()
console.log(`Clear cache request for [${cmd.tag}]`)
let tags = []
if (cmd.tag === '*') {
tags = config.server.availableCacheTags
Expand All @@ -24,17 +23,17 @@ program
return tag.indexOf(t) === 0
})) {
subPromises.push(cache.invalidate(tag).then(() => {
Logger.log(`Tags invalidated successfully for [${tag}]`)()
console.log(`Tags invalidated successfully for [${tag}]`)
}))
} else {
Logger.error(`Invalid tag name ${tag}`)()
console.error(`Invalid tag name ${tag}`)
}
})
Promise.all(subPromises).then(r => {
Logger.log(`All tags invalidated successfully [${cmd.tag}]`)()
console.log(`All tags invalidated successfully [${cmd.tag}]`)
process.exit(0)
}).catch(error => {
Logger.error(error)()
console.error(error)
})
}
})
Expand Down
13 changes: 6 additions & 7 deletions core/scripts/utils/cache-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ const fs = require('fs')
const path = require('path')
const TagCache = require('redis-tag-cache').default
const config = require('config')
let cache = false

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

const cacheVersionPath = path.join(__dirname, '..', 'build', 'cache-version.json')
if (config.server.useOutputCache) {
const cacheVersionPath = path.resolve(path.join('core', '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({
cache = new TagCache({
redis: redisConfig,
defaultTimeout: config.server.outputCacheDefaultTtl
})
}

module.exports = cache

0 comments on commit d09b690

Please sign in to comment.