-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
33 lines (25 loc) · 803 Bytes
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'use strict'
const matcher = require('matcher')
const DATA_POSTFIX = '-d'
const getKeys = (cache, pattern) => new Promise((resolve) => {
if (pattern.indexOf('*') > -1) {
const args = [pattern, (_, res) => resolve(matcher(res, [pattern]))]
if (cache.store.name !== 'redis') {
args.shift()
}
cache.keys.apply(cache, args)
} else resolve([pattern])
})
const get = (cache, key) => cache.getAndPassUp(key)
const deleteKeys = (stores, patterns) => {
patterns = patterns.map(pattern =>
pattern.endsWith('*') ? pattern : [pattern, pattern + DATA_POSTFIX]
).flat()
patterns.forEach(pattern => stores.forEach(store => getKeys(store, pattern).then(keys => keys.length > 0 ? store.del(keys) : null)))
}
module.exports = {
get,
deleteKeys,
getKeys,
DATA_POSTFIX
}