Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
refactor config with fallback version
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-arnoult committed Mar 8, 2022
1 parent a255f3a commit 4c691ec
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit 4c691ec

Please sign in to comment.