Skip to content

Commit 482e958

Browse files
committed
task: add ability to scan cache
1 parent 5b52754 commit 482e958

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/index.js

+35
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const redis = require('redis')
88
const parser = require('parse-redis-url')
9+
const each = require('each')
910

1011
/**
1112
* Module constants.
@@ -150,6 +151,40 @@ class RedisStore {
150151
})
151152
})
152153
}
154+
155+
/**
156+
* Scan for a number of entries from a cursor point
157+
*
158+
* @param {Number} cursor
159+
* @param {Number} fn
160+
* @param {Function} fn
161+
* @api public
162+
*/
163+
164+
scan(cursor, count = 10, fn = noop) {
165+
const entries = []
166+
const prefix = this.prefix
167+
const self = this
168+
169+
this.client.scan(cursor, 'MATCH', `${prefix}*`, 'COUNT', count, (err, data) => {
170+
if (err) return fn(err)
171+
172+
const [newCursor, keys] = data
173+
174+
each(keys).call((key, index, next) => {
175+
const _key = key.replace(`${this.prefix}`, '')
176+
177+
self.get(_key, (err, data) => {
178+
if (err) return fn(err)
179+
180+
entries.push({ key: _key, data })
181+
next()
182+
})
183+
}).next(() => {
184+
fn(null, { cursor: Number(newCursor), entries })
185+
})
186+
})
187+
}
153188
}
154189

155190
module.exports = RedisStore

0 commit comments

Comments
 (0)