2 Star 2 Fork 1

zensh / thunk-redis

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

thunk-redis

A redis client with pipelining, rely on thunks, support promise.

NPM version Build Status Talk topic

thunks

Demo(examples)

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);
});

Benchmark

  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%

Installation

Node.js:

npm install thunk-redis

API(More)

  1. redis.createClient([port], [host], [options])
  2. redis.createClient([path], [options])
  3. redis.log([...])

redis.log

Helper tool, print result or error stack.

var client = redis.createClient();
client.info()(redis.log);

redis.createClient

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.

The MIT License (MIT) Copyright (c) 2014 Yan Qing Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

A redis client with pipelining, rely on thunks, support promise. 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/zensh/thunk-redis.git
git@gitee.com:zensh/thunk-redis.git
zensh
thunk-redis
thunk-redis
master

搜索帮助