Skip to content

Commit 79d0ff8

Browse files
committed
support multiple cache expire patterns
1 parent 0da39c7 commit 79d0ff8

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ service.patch('/numbers', (req, res) => {
9494
})
9595
```
9696

97+
#### Invalidating multiple patterns
98+
Sometimes is required to expire cache entries using multiple patterns, that is also possible using the `,` separator:
99+
```js
100+
res.setHeader('x-cache-expire', '*/pattern1,*/pattern2')
101+
```
102+
97103
### Custom cache keys
98104
Cache keys are generated using: `req.method + req.url`, however, for indexing/segmenting requirements it makes sense to allow cache keys extensions.
99105

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ const middleware = (opts) => async (req, res, next) => {
5050
onEnd(res, (payload) => {
5151
if (payload.headers[X_CACHE_EXPIRE]) {
5252
// support service level expiration
53-
const keysPattern = payload.headers[X_CACHE_EXPIRE]
53+
const keysPattern = payload.headers[X_CACHE_EXPIRE].replace(/\s/g, '')
54+
const patterns = keysPattern.split(',')
5455
// delete keys on all cache tiers
55-
opts.stores.forEach(cache => getKeys(cache, keysPattern).then(keys => mcache.del(keys)))
56+
patterns.forEach(pattern =>
57+
opts.stores.forEach(cache =>
58+
getKeys(cache, pattern).then(keys =>
59+
mcache.del(keys))))
5660
} else if (payload.headers[X_CACHE_TIMEOUT]) {
5761
// we need to cache response
5862
mcache.set(req.cacheKey, JSON.stringify(payload), {

0 commit comments

Comments
 (0)