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 6dfa034
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions packages/redis/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,29 @@ class KeyvRedis {
return result > 0
}

// async clear (namespace) {
// const match = namespace ? `${namespace}:*` : '*'
// const stream = this.redis.scanStream({ match })
// const keys = []
// const collectKeys = new Transform({
// objectMode: true,
// transform (chunk, _, next) {
// keys.push.apply(keys, chunk)
// next()
// }
// })
// await pipeline(stream, collectKeys)
// if (keys.length > 0) await this.redis.unlink(keys)
// }

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)
next()
}
transform: (chunk, _, next) => chunk.length > 0 ? this.redis.unlink(chunk).then(() => next()) : 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 6dfa034

Please sign in to comment.