A redis client with pipelining, rely on thunks, support promise.
default thunk API:
var redis = require('thunk-redis');
var client = redis.createClient({
database: 0
});
client.on('connect', function() {
console.log('redis connected!');
});
client.info('server')(function(error, res) {
console.log('redis server info:', res);
return this.dbsize();
})(function(error, res) {
console.log('surrent database size:', res);
return this.select(0);
})(function(error, res) {
console.log('select database 0:', res);
return this.quit();
})(function(error, res) {
console.log('redis client quit:', res);
});
use promise API:
var redis = require('thunk-redis');
var client = redis.createClient({
database: 1,
usePromise: true
});
client.on('connect', function() {
console.log('redis connected!');
});
client
.info('server')
.then(function(res) {
console.log('redis server info:', res);
return client.dbsize();
})
.then(function(res) {
console.log('surrent database size:', res);
return client.select(0);
})
.then(function(res) {
console.log('select database 0:', res);
return client.quit();
})
.then(function(res) {
console.log('redis client quit:', res);
});
support generator in thunk API:
var redis = require('thunk-redis');
var client = redis.createClient();
client.select(1)(function*(error, res) {
console.log(error, res);
yield this.set('foo', 'bar');
yield this.set('bar', 'baz');
console.log('foo -> %s', yield this.get('foo'));
console.log('bar -> %s', yield this.get('bar'));
var user = {
id: 'u001',
name: 'jay',
age: 24
};
// transaction, it is different from node_redis!
yield [
this.multi(),
this.set(user.id, JSON.stringify(user)),
this.zadd('userAge', user.age, user.id),
this.pfadd('ageLog', user.age),
this.exec()
];
return this.quit();
})(function(error, res) {
console.log(error, res);
});
➜ thunk-redis git:(master) ✗ node --harmony benchmark/index.js
redis(N):node_redis
OK
redis(T):thunk-redis
OK
Start...
redis(N): PING 49358 ops/sec 100%
redis(T): PING 54495 ops/sec 110.4%
redis(N): SET small string 39062 ops/sec 100%
redis(T): SET small string 44523 ops/sec 114.0%
redis(N): GET small string 43859 ops/sec 100%
redis(T): GET small string 47687 ops/sec 108.7%
redis(N): SET long string 28320 ops/sec 100%
redis(T): SET long string 35323 ops/sec 124.7%
redis(N): GET long string 30432 ops/sec 100%
redis(T): GET long string 26645 ops/sec 87.6%
redis(N): INCR 46061 ops/sec 100%
redis(T): INCR 48756 ops/sec 105.9%
redis(N): LPUSH 39824 ops/sec 100%
redis(T): LPUSH 45289 ops/sec 113.7%
redis(N): LRANGE 100 8322 ops/sec 100%
redis(T): LRANGE 100 10094 ops/sec 121.3%
Node.js:
npm install thunk-redis
Helper tool, print result or error stack.
var client = redis.createClient();
client.info()(redis.log);
var client1 = redis.createClient();
var client2 = redis.createClient({database: 2});
var client3 = redis.createClient(6379, {database: 2});
var client4 = redis.createClient(6379, '127.0.0.1', {database: 2});
var client5 = redis.createClient('/tmp/redis.sock');
var client6 = redis.createClient('/tmp/redis.sock', {database: 2});
options.authPass
: Optional, Type: String
, Default: ''
.
options.database
: Optional, Type: Number
, Default: 0
.
options.debugMode
: Optional, Type: Boolean
, Default: false
.
Print request data and response data.
options.returnBuffers
: Optional, Type: Boolean
, Default: false
.
options.usePromise
: Optional, Type: Boolean
or Promise
constructor, Default: false
.
Export promise commands API.
Use default Promise:
var redis = require('thunk-redis');
var client = redis.createClient({
database: 1,
usePromise: true
});
Use bluebird:
var redis = require('thunk-redis');
var Bluebird = require('bluebird');
var client = redis.createClient({
database: 1,
usePromise: Bluebird
});
options.noDelay
: Optional, Type: Boolean
, Default: true
.
Disables the Nagle algorithm. By default TCP connections use the Nagle algorithm, they buffer data before sending it off. Setting true for noDelay will immediately fire off data each time socket.write() is called.
options.keepAlive
: Optional, Type: Boolean
, Default: true
.
Enable/disable keep-alive functionality, and optionally set the initial delay before the first keepalive probe is sent on an idle socket.
options.timeout
: Optional, Type: Number
, Default: 0
.
Sets the socket to timeout after timeout milliseconds of inactivity on the socket. If timeout is 0, then the existing idle timeout is disabled.
When an idle timeout is triggered the socket will receive a 'timeout' event but the connection will not be severed.
options.retryDelay
: Optional, Type: Number
, Default: 5000
.
options.maxAttempts
: Optional, Type: Number
, Default: 5
.
options.commandsHighWater
: Optional, Type: Number
, Default: 10000
.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型