使用Node.js和bluebird读取Redis数据
我用bluebird重写了以下程序。使用Node.js:Async/Await来读取Redis数据。
安装图书馆。
sudo npm install -g bluebird
#! /usr/bin/node
// ---------------------------------------------------------------
//
// bluebird_read.js
//
// Dec/30/2020
// ---------------------------------------------------------------
'use strict'
const bluebird = require("bluebird")
const redis = require('redis')
bluebird.promisifyAll(redis.RedisClient.prototype)
bluebird.promisifyAll(redis.Multi.prototype)
// ---------------------------------------------------------------
async function proc01 ()
{
const redisUrl = 'redis://127.0.0.1:6379'
const client = redis.createClient(redisUrl)
const keys = await client.keysAsync('*')
keys.forEach(async function(key,index)
{
const value = await client.getAsync(key)
console.log(key,value)
})
await client.quitAsync()
}
// ---------------------------------------------------------------
console.error ("*** 開始 ***")
proc01()
console.error ("*** 終了 ***")
// ---------------------------------------------------------------
执行脚本
export NODE_PATH=/usr/lib/node_modules
./bluebird_read.js