Skip to content

Commit

Permalink
perf(redis): clear while scan
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Apr 28, 2024
1 parent b1e246d commit f5c667d
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions packages/redis/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,14 @@ class KeyvRedis {
async clear (namespace) {
const match = namespace ? `${namespace}:*` : '*'
const stream = this.redis.scanStream({ match })
const keys = []
const collectKeys = new Transform({
const unlinkKeys = new Transform({
objectMode: true,
transform (chunk, _, next) {
keys.push.apply(keys, chunk)
transform: async (chunk, _, next) => {
await this.redis.unlink(chunk)
next()
}
})
await pipeline(stream, collectKeys)
if (keys.length > 0) await this.redis.unlink(keys)
await promisify(pipeline)(stream, unlinkKeys)
}

async * iterator (namespace) {
Expand Down

0 comments on commit f5c667d

Please sign in to comment.