From a255f3a1f32699b4b4045d85a02f05b5f46e528a Mon Sep 17 00:00:00 2001 From: Marc Arnoult Date: Fri, 18 Feb 2022 00:08:59 +0100 Subject: [PATCH 1/2] feat(chore): add default versioning & hostname on cache key --- src/index.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 7e844cf..90c26b7 100644 --- a/src/index.js +++ b/src/index.js @@ -1,11 +1,17 @@ import Redis from 'redis-tag-cache'; +import crypto from 'crypto'; -export default function RedisCache (options) { - const client = new Redis(options); +const defaultVersion = crypto.randomBytes(15).toString('hex') +export default function RedisCache (options) { + const client = new Redis(options.redisConfig); + return { - async invoke({ route, render, getTags }) { - const key = `page:${ route }`; + async invoke({ route, context, render, getTags }) { + const version = options.version || defaultVersion + const hostname = context.req.hostname + + const key = `${ version }:page:${ hostname }${ route }`; const cachedResponse = await client.get(key); if (cachedResponse) { From 4c691ec8af666c7bab05212cb51deea830de1e0b Mon Sep 17 00:00:00 2001 From: Marc Arnoult Date: Tue, 8 Mar 2022 16:52:56 +0100 Subject: [PATCH 2/2] refactor config with fallback version --- src/index.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 90c26b7..e3412df 100644 --- a/src/index.js +++ b/src/index.js @@ -1,17 +1,24 @@ import Redis from 'redis-tag-cache'; import crypto from 'crypto'; +import { Logger } from '@vue-storefront/core' -const defaultVersion = crypto.randomBytes(15).toString('hex') export default function RedisCache (options) { - const client = new Redis(options.redisConfig); + const { version, ...redisConfig } = options; + + const client = new Redis(redisConfig); + const fallbackVersion = crypto.randomBytes(15).toString('hex') return { async invoke({ route, context, render, getTags }) { - const version = options.version || defaultVersion + const cacheVersion = version || fallbackVersion const hostname = context.req.hostname - const key = `${ version }:page:${ hostname }${ route }`; + if (!version) { + Logger.warn('The `version` property is missing in the `@vue-storefront/redis-cache` package configuration. In a multi-instance setup, this will result in a separate cache for every instance. Please refer to Redis driver documentation for more details.'); + } + + const key = `${ cacheVersion }:page:${ hostname }${ route }`; const cachedResponse = await client.get(key); if (cachedResponse) {