Nodejs的redis模块更新,支持hiredis了
Nodejs的redis模块更新,支持hiredis了
###安装 This is a complete Redis client for node.js. It supports all Redis commands, including many recently added commands like EVAL from experimental Redis server branches.
Install with:
npm install redis
Pieter Noordhuis has provided a binding to the official hiredis C library, which is non-blocking and fast. To use hiredis, do:
npm install hiredis redis
If hiredis is installed, node_redis will use it by default. Otherwise, a pure JavaScript parser will be used.
If you use hiredis, be sure to rebuild it whenever you upgrade your version of node. There are mysterious failures that can happen between node and native code modules after a node upgrade.
###性能测试 JavaScript parser:
PING: 20000 ops 42283.30 ops/sec 0/5/1.182
SET: 20000 ops 32948.93 ops/sec 1/7/1.515
GET: 20000 ops 28694.40 ops/sec 0/9/1.740
INCR: 20000 ops 39370.08 ops/sec 0/8/1.269
LPUSH: 20000 ops 36429.87 ops/sec 0/8/1.370
LRANGE (10 elements): 20000 ops 9891.20 ops/sec 1/9/5.048
LRANGE (100 elements): 20000 ops 1384.56 ops/sec 10/91/36.072
hiredis parser:
PING: 20000 ops 46189.38 ops/sec 1/4/1.082
SET: 20000 ops 41237.11 ops/sec 0/6/1.210
GET: 20000 ops 39682.54 ops/sec 1/7/1.257
INCR: 20000 ops 40080.16 ops/sec 0/8/1.242
LPUSH: 20000 ops 41152.26 ops/sec 0/3/1.212
LRANGE (10 elements): 20000 ops 36563.07 ops/sec 1/8/1.363
LRANGE (100 elements): 20000 ops 21834.06 ops/sec 0/9/2.287
The performance of node_redis improves dramatically with pipelining, which happens automatically in most normal programs.
Nodejs的redis模块更新,支持hiredis了
安装
node_redis
是一个完整的 Redis 客户端库,适用于 Node.js。它支持所有 Redis 命令,包括最近添加的一些实验性命令,如来自实验性 Redis 服务器分支的 EVAL。
首先,安装 node_redis
:
npm install redis
Pieter Noordhuis 提供了一个与官方 hiredis C 库绑定的版本。hiredis
是一个非阻塞且快速的解析器。为了使用 hiredis
,你需要分别安装 hiredis
和 redis
:
npm install hiredis redis
如果安装了 hiredis
,node_redis
将默认使用它。如果没有安装,则会使用纯 JavaScript 解析器。
注意: 如果你使用 hiredis
,确保在升级 Node.js 版本后重新编译它,以避免神秘的故障问题。
性能测试
以下是使用纯 JavaScript 解析器和 hiredis
解析器的性能对比:
JavaScript 解析器
PING: 20000 ops 42283.30 ops/sec 0/5/1.182
SET: 20000 ops 32948.93 ops/sec 1/7/1.515
GET: 20000 ops 28694.40 ops/sec 0/9/1.740
INCR: 20000 ops 39370.08 ops/sec 0/8/1.269
LPUSH: 20000 ops 36429.87 ops/sec 0/8/1.370
LRANGE (10 elements): 20000 ops 9891.20 ops/sec 1/9/5.048
LRANGE (100 elements): 20000 ops 1384.56 ops/sec 10/91/36.072
hiredis 解析器
PING: 20000 ops 46189.38 ops/sec 1/4/1.082
SET: 20000 ops 41237.11 ops/sec 0/6/1.210
GET: 20000 ops 39682.54 ops/sec 1/7/1.257
INCR: 20000 ops 40080.16 ops/sec 0/8/1.242
LPUSH: 20000 ops 41152.26 ops/sec 0/3/1.212
LRANGE (10 elements): 20000 ops 36563.07 ops/sec 1/8/1.363
LRANGE (100 elements): 20000 ops 21834.06 ops/sec 0/9/2.287
可以看到,在大多数操作中,使用 hiredis
的性能都有显著提升,特别是在处理大量数据时(例如 LRANGE 100 元素)。
你可以通过以下代码片段来体验这些性能改进:
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => {
console.error(`Error: ${err}`);
});
// 使用 hiredis
client.hgetall('myhash', (err, replies) => {
if (err) throw err;
console.log(replies);
});
这段代码展示了如何创建一个 Redis 客户端,并从名为 myhash
的哈希表中获取所有键值对。如果你已经安装了 hiredis
,那么这段代码将自动使用它来提高性能。
更多关于 node_redis
的详细信息可以参阅 GitHub 仓库。
顶
顶!这个库超稳定,用起来很爽……
这个是今年的帖子??话说…前几个月就支持了啊…
Nodejs的redis模块更新,支持hiredis了
安装
node_redis
是一个完整的 Redis 客户端库,支持所有 Redis 命令。你可以通过以下命令安装:
npm install redis
Pieter Noordhuis 提供了一个与官方 hiredis
C 库绑定的版本,它提供了非阻塞和高性能的支持。要使用 hiredis
,需要执行以下命令:
npm install hiredis redis
如果已经安装了 hiredis
,那么 node_redis
将默认使用它。否则,将会使用纯 JavaScript 解析器。
如果你选择使用 hiredis
,请确保每次升级 Node.js 版本后重新编译它,以避免一些神秘的错误。
性能测试
使用 hiredis
可以显著提升性能。以下是使用 JavaScript 解析器和 hiredis
解析器的性能对比:
JavaScript 解析器
PING: 20000 ops 42283.30 ops/sec 0/5/1.182
SET: 20000 ops 32948.93 ops/sec 1/7/1.515
GET: 20000 ops 28694.40 ops/sec 0/9/1.740
INCR: 20000 ops 39370.08 ops/sec 0/8/1.269
LPUSH: 20000 ops 36429.87 ops/sec 0/8/1.370
LRANGE (10 elements): 20000 ops 9891.20 ops/sec 1/9/5.048
LRANGE (100 elements): 20000 ops 1384.56 ops/sec 10/91/36.072
hiredis 解析器
PING: 20000 ops 46189.38 ops/sec 1/4/1.082
SET: 20000 ops 41237.11 ops/sec 0/6/1.210
GET: 20000 ops 39682.54 ops/sec 1/7/1.257
INCR: 20000 ops 40080.16 ops/sec 0/8/1.242
LPUSH: 20000 ops 41152.26 ops/sec 0/3/1.212
LRANGE (10 elements): 20000 ops 36563.07 ops/sec 1/8/1.363
LRANGE (100 elements): 20000 ops 21834.06 ops/sec 0/9/2.287
可以看出,使用 hiredis
的解析器在大多数操作上都表现出更高的性能。
示例代码
下面是一个简单的示例代码,展示如何使用 node_redis
和 hiredis
:
const redis = require('redis');
const hiredis = require('hiredis');
// 创建 Redis 客户端
const client = redis.createClient();
client.on('error', (err) => {
console.error(`Redis error: ${err}`);
});
client.set('testKey', 'testValue', (err, reply) => {
if (err) throw err;
console.log(reply); // 会输出 OK
});
client.get('testKey', (err, reply) => {
if (err) throw err;
console.log(reply); // 会输出 testValue
});
确保在运行这段代码前已经安装了 redis
和 hiredis
。