ioredis 使用管道删除键

ioredis using pipeline to delete keys

我正在尝试使用 ioredis (nodejs) 通过 pipeline 方法删除多个键。

但它对我在redis-cli中的密钥没有影响。

我的代码:

    const Redis = require('ioredis')
    const redis = new Redis()
    redis.scanStream({ match: 'myprefix:*' })
        .on('data', async(keys) => {
            if (keys.length) {
                const pipeline = this.store.pipeline()
                keys.forEach((key) => {
                  pipeline.del(key)
                })
                await pipeline.exec((err, results) => {
                  console.log(results) // returns [ [ null, 0 ], [ null, 0 ], [ null, 0 ], [ null, 0 ], [ null, 0 ] ]
                  console.log(err) // returns null
                })
            }
        })
        .on('end', () => {
            console.log("end") // returns "end"
        }

我还测试了以下代码:它也什么都不做:

    ...
    const pipeline = this.store.pipeline()
    await pipeline.del('anotherprefix:test')
    await pipeline.exec((err, results) => {
        console.log(results) // returns [ [ null, 0 ] ]
        console.log(err) // returns null
    })
    ...

解决方案是从删除请求中删除前缀。

然后做例如:

const Redis = require('ioredis')
const redis = new Redis()
const prefix = 'myprefix'

redis.scanStream({ match: prefix +':*' })
    .on('data', async(keys) => {
        if (keys.length) {
            const pipeline = this.store.pipeline()
            keys.forEach((key) => {
              pipeline.del(key.replace(prefix + ':', '')
            })
            await pipeline.exec()
        }
    })
    .on('end', () => {
        ...
    }