每个项目产品都会让你加埋点,你是愿意花几天一个个加,还是愿意几分钟一个小时加完去喝茶聊天?来试试这520web工具, 高效加埋点,目前我们公司100号前端都在用,因为很好用,所以很自然普及开来了,推荐给大家吧
自己开发所以免费,埋点越多越能节约时间,点两下埋点就加上了,还不会犯错,里面有使用视频,反正免费 😄
Introduction
ioredis.js is a new, performance focused Redis client for Node.js. It is used by the online commerce company, Alibaba. It also supports ES6 types such as Map and Set and also implements support for Promises, which the other big Redis client, <a href=“https://github.com/mranney/node_redis”>node_redis</a>, is not. <a href=“https://github.com/luin/ioredis”>ioredis.js</a> is newly released so in the next sections of this blog post I will show you some basic usage of this client.
Installing the client
npm install ioredis
Instantiating the client
var Redis = require("ioredis");
//basic constructor - Connect to 127.0.0.1:6379
var redis = new Redis();
There are multiple ways to instantiate the Redis client. Above I presented the basic method of doing it and bellow I will show you more methods:
//127.0.0.1:6400
var Redis = new Redis(6400);
// 192.168.1.1:6379
var Redis = new Redis(6379, '192.168.1.1');
// 192.168.1.1:6379
var Redis = new Redis(6379, '192.168.1.1');
// 127.0.0.1:6380, db 4
new Redis('redis://:authpassword@127.0.0.1:6380/4');
new 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
})
I also suggest, as best practice in general to use environment variables for connectivity credentials/urls/ports.
Basic functionality
The two basic Redis operations set and get can be done pretty straight forward with ioredis.js.
Set:
redis.set('key', 'value');
Get using callbacks:
redis.get('key', function (err, result) {
console.log(result); //will display 'value'
});
Get using promises:
redis.get('key').then(function (result) {
console.log(result); //will display 'value'
});
Adding a set of values:
// Arguments to commands are flatten, so the following are same
redis.sadd('set', 1, 3, 5, 7);
// or
redis.sadd('set', [1, 3, 5, 7]);
Getting a set of values using callback:
redis.smembers('set', function(err, result) {
console.log(result); //will display '1, 3, 5, 7'
});
Getting a set of values using promises:
redis.smembers('set').then(function(result) {
console.log(result); //will display '1, 3, 5, 7'
});
All Redis commands are supported by ioredis.js, both using callbacks or promises. Just use each command as a method name for the Redis client.
Handling binary data
ioredis.js also handles binary data by adding and retrieving buffers.
Adding:
redis.set('key', new Buffer('value'));
Every command has a method that returns a buffer. By doing that you have to append “Buffer” to the command name.
Retrieving:
redis.getBuffer('key', function (err, result) {
// result is a buffer.
});
ioredis.js also supports pipelining, transactions, offline queues, Sentinel and Cluster. For these advanced operations you can find a pretty detailed description here.
I hope this tutorial is informative enough to give you a start with ioredis.js.