当前位置:   article > 正文

nodejs cluster_NodeJS下好用的Redis客户端ioredis,再推荐一个Redis可视化工具

nodejs ioredis cluster

介绍

ioredis是一个适用于Nodejs的Redis全功能客户端,健壮性以及高性能是它的亮点,支持Redis> = 2.6.12和(Node.js> = 6),ioredis是一个功能强大的功能强大的Redis客户,已被阿里巴巴和许多其他了不起的公司所使用。


8d1d631ddc9a1c588781c5c59e42f6a7.png

Github

https://github.com/luin/ioredis

特性

功能齐全。它支持Cluster,Sentinel,Pipelining,当然还支持Lua脚本和Pub / Sub(具有二进制消息的支持)高性能易于使用的API。它与Node回调和本机Promise一起使用命令参数和回复的转换透明键前缀Lua脚本的抽象,允许您定义自定义命令支持二进制数据支持TLS支持脱机队列和就绪检查支持ES6类型,例如Map和Set支持GEO命令(Redis 3.2不稳定)复杂的错误处理策略支持NAT映射

安装

npm install ioredis

基本使用

var Redis = require("ioredis");var redis = new Redis();redis.set("foo", "bar");redis.get("foo", function(err, result) { console.log(result);});redis.del("foo");// 或者如果最后一个参数不是函数,则使用Promiseredis.get("foo").then(function(result) { console.log(result);});redis.sadd("set", 1, 3, 5, 7);redis.sadd("set", [1, 3, 5, 7]);// 所有参数都直接传递给redis服务器:redis.set("key", 100, "EX", 10);

连接到Redis

创建新的Redis实例后,将同时创建到Redis的连接。可以通过以下方式指定要连接的Redis:

new Redis(); // Connect to 127.0.0.1:6379new Redis(6380); // 127.0.0.1:6380new Redis(6379, "192.168.1.1"); // 192.168.1.1:6379new Redis("/tmp/redis.sock");new Redis({ port: 6379, // Redis port host: "127.0.0.1", // Redis host family: 4, // 4 (IPv4) or 6 (IPv6) password: "auth", db: 0});

使用TLS加密时,还可以将连接选项指定为redis:// URL或rediss:// URL:

new Redis("redis://:authpassword@127.0.0.1:6380/4");

发布/订阅

这是发布/订阅API的简单示例。以下程序将打开两个客户端连接。它订阅一个连接的频道,然后发布另一个连接:

var Redis = require("ioredis");var redis = new Redis();var pub = new Redis();redis.subscribe("news", "music", function(err, count) { // 现在我们都订阅了“新闻”和“音乐”频道。 // `count` 代表我们当前订阅的频道数。 pub.publish("news", "Hello world!"); pub.publish("music", "Hello again!");});redis.on("message", function(channel, message) { // 从新闻频道,收到消息Hello world! // 从音乐频道再次收到消息Hello! console.log("Receive message %s from channel %s", message, channel);});//还有一个称为“ messageBuffer”的事件,与“ message”相同,除了//返回缓冲区buffer而不是字符串。redis.on("messageBuffer", function(channel, message) { });

还以类似方式支持PSUBSCRIBE:

redis.psubscribe("pat?ern", function(err, count) {});redis.on("pmessage", function(pattern, channel, message) {});redis.on("pmessageBuffer", function(pattern, channel, message) {});

当客户端发出SUBSCRIBE或PSUBSCRIBE时,该连接将进入“订阅者”模式。那时,只有修改订阅集的命令才有效。当订阅集为空时,连接将恢复为常规模式。如果需要在订阅者模式下向Redis发送常规命令,只需打开另一个连接即可。

处理二进制数据

redis.set("foo", Buffer.from("bar"));

每个命令都有一个返回Buffer的方法(通过在命令名后添加“ Buffer”后缀)。获取缓冲区buffer:

redis.getBuffer("foo", function(err, result) { // result is a buffer.});

Pipelining

如果要发送一批命令(例如> 5),则可以使用流水线将命令在内存中排队,然后将它们一次全部发送到Redis。这样,性能提高了50%〜300%。redis.pipeline()创建一个Pipeline实例。您可以像Redis实例一样在其上调用任何Redis命令。这些命令在内存中排队,并通过调用exec方法刷新到Redis:

var pipeline = redis.pipeline();pipeline.set("foo", "bar");pipeline.del("cc");pipeline.exec(function(err, results) { });redis .pipeline() .set("foo", "bar") .del("cc") .exec(function(err, results) {});var promise = redis .pipeline() .set("foo", "bar") .get("foo") .exec();promise.then(function(result) {});

每个链接的命令还可以具有一个回调,该回调将在命令得到答复时被调用:

redis .pipeline() .set("foo", "bar") .get("foo", function(err, result) { // result === 'bar' }) .exec(function(err, result) { // result[1][1] === 'bar' });

除了将命令分别添加到管道队列之外,还可以将命令和参数数组传递给构造函数:

redis.pipeline([["set", "foo", "bar"], ["get", "foo"]]).exec(function() { /* ... */});

length属性显示管道中有多少个命令:

const length = redis .pipeline() .set("foo", "bar") .get("foo").length;// length === 2

。。。。。。

以上是借鉴Github上介绍的部分功能,如果需要详细了解它的所有功能,则可以直接参考Github上提供的文档API,非常详细,本文篇幅有限就不在此展示!

https://github.com/luin/ioredis

Redis可视化客户端

之前的文章中介绍了很多Redis的可视化管理客户端,今天就在介绍一个跨平台的Redis GUI——Medis。Medis是一个漂亮的,易于使用的Redis管理应用程序,它基于Electron,React和Redux构建在现代网络上。它由许多很棒的Node.js模块提供支持,尤其是ioredis和ssh2。


9750ae8bfbe3e982acf0454df7af7071.png

特性:

按键查看/编辑SSH隧道,用于连接远程服务器执行自定义命令的终端配置查看/编辑

高级:

JSON / MessagePack格式查看/编辑和内置高亮显示/验证在不阻止Redis服务器的情况下使用数百万个键和键成员模式管理器,可轻松选择子键组。
https://github.com/luin/medis

总结

如果你正愁在Nodejs下没有找到一个好用的Redis客户端,那么ioredis则是你可以选择的一个选项,好不好用还得试试才知道,Enjoy it!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/410513
推荐阅读
相关标签
  

闽ICP备14008679号