Skip to content

Commit 1cb3499

Browse files
authored
Merge pull request #2 from jkyberneees/support-multiple-expire-patterns
Support multiple expire patterns
2 parents 0da39c7 + c51a53d commit 1cb3499

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
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), {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "http-cache-middleware",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "HTTP Cache Middleware",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)