节点中的 Redis - express。如何在不初始化客户端的情况下扫描密钥

Redis in node - express. How to scan keys without initializing a client

我有以下扫描器

const scan = promisify(redisClient1.scan).bind(redisClient1);

const scanAll = async (pattern) => {
    const found = [];
    let cursor = '0';

    do {
        const reply = await scan(cursor, 'MATCH', pattern);
        cursor = reply[0];
        found.push(...reply[1]);
    } while (cursor !== '0');

    return found;
};

但是要使用它我必须每次都初始化 redisClient1

const redisClient1 = require('redis').createClient(config.redisPort, config.redisUrl, {
    no_ready_check: true,
    db: 1
});


redisClient1.on('error', function (err) {
    console.log('Error ' + err);
});

redisClient1.on('connect', function () {
    console.log('Connected to Redis pre/dev.');
});

问题是,我需要 scanAll 函数将 redisPort 和 redisUrl 作为参数(db 始终为 1)

因此一旦函数接收到参数,客户端初始化就应该发生

意味着它看起来像这样

const scanAll = async (url, port) => {
        const found = [];
        let cursor = '0';
    
        do {
            const reply = await customScan(url+port;, cursor, 'Match', pattern)            
            cursor = reply[0];
            found.push(...reply[1]);
        } while (cursor !== '0');
    
        return found;
    };

我怎样才能做类似的事情?

我最终得到了这个

const createRedisClient = async (port, url) => {
    const redisClient = require('redis').createClient(config.redisPort, config.redisUrl, {
        no_ready_check: true,
        db: 1
    })
    return redisClient;
}

const scanAll = async (pattern) => {
    const redisClient = await createRedisClient('1111', 'server.com')
    const scan = promisify(redisClient.scan).bind(redisClient);
    const found = [];
    let cursor = '0';
    do {
        const reply = await scan(cursor, 'MATCH', pattern);
        cursor = reply[0];
        found.push(...reply[1]);
    } while (cursor !== '0');
    return found;
};


const serverInfo = async () => {
    const redisClient = await createRedisClient('1111', 'server.com')
    const info = promisify(redisClient.info).bind(redisClient);
    const reply = await info();
    return reply;
}