From 2ca8b8e3d43d788bf0024d29a53359ce2ef911ab Mon Sep 17 00:00:00 2001 From: Rolando Santamaria Maso Date: Tue, 25 May 2021 14:32:17 +0200 Subject: [PATCH] fixing wildcard pattern support --- index.js | 15 ++++++++++----- test/smoke.test.js | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index eddf2b5..c44cb9a 100644 --- a/index.js +++ b/index.js @@ -76,13 +76,18 @@ const middleware = (opts) => async (req, res, next) => { if (payload.headers[X_CACHE_EXPIRE]) { // support service level expiration const keysPattern = payload.headers[X_CACHE_EXPIRE].replace(/\s/g, '') - const patterns = keysPattern.split(',').map(pattern => - pattern.endsWith('*') ? pattern : [pattern, pattern + DATA_POSTFIX]) - .reduce((acc, item) => { + const patterns = keysPattern.split(',').map(pattern => pattern.endsWith('*') + ? pattern + : [pattern, pattern + DATA_POSTFIX] + ).reduce((acc, item) => { + if (Array.isArray(item)) { acc.push(...item) + } else { + acc.push(item) + } - return acc - }, []) + return acc + }, []) // delete keys on all cache tiers patterns.forEach(pattern => opts.stores.forEach(store => getKeys(store, pattern).then(keys => mcache.del(keys)))) } else if (payload.headers[X_CACHE_TIMEOUT] || payload.headers[CACHE_CONTROL]) { diff --git a/test/smoke.test.js b/test/smoke.test.js index 1cea149..069cf6e 100644 --- a/test/smoke.test.js +++ b/test/smoke.test.js @@ -61,6 +61,11 @@ describe('cache middleware', () => { res.setHeader('x-cache-expire', '*/cache') res.end() }) + + server.delete('/cache2', (req, res) => { + res.setHeader('x-cache-expire', '*/cache*') + res.end() + }) }) it('start', async () => { @@ -94,7 +99,17 @@ describe('cache middleware', () => { await got.delete('http://localhost:3000/cache') }) - it('create cache 2', async () => { + it('re-create cache', async () => { + const res = await got('http://localhost:3000/cache') + expect(res.body).to.equal('hello') + expect(res.headers['x-cache-hit']).to.equal(undefined) + }) + + it('cache expire using wildcard', async () => { + await got.delete('http://localhost:3000/cache2') + }) + + it('re-create cache', async () => { const res = await got('http://localhost:3000/cache') expect(res.body).to.equal('hello') expect(res.headers['x-cache-hit']).to.equal(undefined)