Skip to content

fixing wildcard pattern support #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down
17 changes: 16 additions & 1 deletion test/smoke.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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)
Expand Down