看看node.js訪問redis的庫

在公司需要分享一下redis的技術,嘗試過php,c,java訪問的實例,接下來需要看看node.js如何實現訪問的

首先在redis.io網站的client找到下載頁

https://github.com/mranney/node_redis

https://github.com/mjijackson/then-redis

有兩個版本可用

第一步:

首先測試node_redis,這個版本提供的client支持功能完善

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.【下載master.zip中的readme的內容】

步驟如下

1:啓動redis在Centos下

192.168.0.107,6379

2:將 編譯好的node.exe複製到node-redis目錄下

D:\pbase\node_redis\

首先修改test.js中的

/*global require console setTimeout process Buffer */
var PORT = 6379;
var HOST = '192.168.0.107';

然後運行node.exe example/simple.js

運行報錯

huareal@gpx /cygdrive/d/pbase/node_redis
# ./node.exe examples/simple.js

module.js:340
    throw err;
          ^
Error: Cannot find module 'redis'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (D:\pbase\node_redis\examples\simple.js:1:75)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

直接運行./node.exe test.js

# ./node.exe test.js
Connected to 192.168.0.107:6379, Redis server version 2.7.102

Using reply parser javascript
- flushdb: 123 ms
- incr: 6 ms
- multi_1: 30 ms
- multi_2: 9 ms
- multi_3: 10 ms
- multi_4: 6 ms
- multi_5: 5 ms
- multi_6: 10 ms
- multi_7: 24 ms
- multi_exception_1: 8 ms
- fwd_errors_1:incoming
 174 ms
- eval_1: 68 ms
- script_load: 44 ms
- client_list:addr=192.168.0.100:4569 fd=6 name= age=1 idle=0 flags=N db=15 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events
addr=192.168.0.100:4570 fd=7 name= age=1 idle=0 flags=N db=15 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=select
addr=192.168.0.100:4571 fd=8 name= age=1 idle=0 flags=N db=15 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=subscribe
addr=192.168.0.100:4572 fd=9 name= age=1 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client

addr=192.168.0.100:4569 fd=6 name= age=1 idle=0 flags=x db=15 sub=0 psub=0 multi=1 qbuf=0 qbuf-free=32768 obl=4 oll=0 omem=0 events=rw cmd=exec
addr=192.168.0.100:4570 fd=7 name= age=1 idle=0 flags=N db=15 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=select
addr=192.168.0.100:4571 fd=8 name= age=1 idle=0 flags=N db=15 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=subscribe
addr=192.168.0.100:4572 fd=9 name= age=1 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client

addr=192.168.0.100:4569 fd=6 name= age=1 idle=0 flags=x db=15 sub=0 psub=0 multi=1 qbuf=0 qbuf-free=32768 obl=4 oll=0 omem=0 events=rw cmd=exec
addr=192.168.0.100:4570 fd=7 name= age=1 idle=0 flags=N db=15 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=select
addr=192.168.0.100:4571 fd=8 name= age=1 idle=0 flags=N db=15 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=subscribe
addr=192.168.0.100:4572 fd=9 name= age=1 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client

 12 ms
- watch_multi: 6 ms
- watch_transaction: 29 ms
- detect_buffers:Uncaught exception: Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
    at RedisClient.on_error (D:\pbase\node_redis\index.js:166:24)
    at Socket.<anonymous> (D:\pbase\node_redis\index.js:89:14)
    at Socket.EventEmitter.emit (events.js:95:17)
    at net.js:441:14
    at process._tickCallback (node.js:417:13)

assert.js:92
  throw new assert.AssertionError({
        ^
AssertionError: true == false
    at process.<anonymous> (D:\pbase\node_redis\test.js:2066:12)
    at process.EventEmitter.emit (events.js:95:17)
    at process.exit (node.js:709:17)
    at process.<anonymous> (D:\pbase\node_redis\test.js:2061:13)
    at process.EventEmitter.emit (events.js:95:17)

實例是OK,還是有錯誤,莫不是自己編譯的版本有問題。替換下載版本測試。

測試另外一個下載版本

# ./node.exe node_redis/test.js
Connected to 192.168.0.107:6379, Redis server version 2.7.102

Using reply parser javascript
- flushdb: 3 ms
- incr: 3 ms
- multi_1: 6 ms
- multi_2: 3 ms
- multi_3: 3 ms
- multi_4: 2 ms
- multi_5: 2 ms
- multi_6: 3 ms
- multi_7: 2 ms
- multi_exception_1: 3 ms
- fwd_errors_1:incoming
 155 ms
- eval_1: 38 ms
- script_load: 2 ms
- client_list:addr=192.168.0.100:4585 fd=6 name= age=4 idle=1 flags=N db=15 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=select
addr=192.168.0.100:4586 fd=7 name= age=4 idle=1 flags=N db=15 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=subscribe
addr=192.168.0.100:4587 fd=8 name= age=4 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
addr=192.168.0.100:4584 fd=9 name= age=1 idle=0 flags=N db=15 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=exec

addr=192.168.0.100:4585 fd=6 name= age=4 idle=1 flags=N db=15 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=select
addr=192.168.0.100:4586 fd=7 name= age=4 idle=1 flags=N db=15 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=subscribe
addr=192.168.0.100:4587 fd=8 name= age=4 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
addr=192.168.0.100:4584 fd=9 name= age=1 idle=0 flags=x db=15 sub=0 psub=0 multi=1 qbuf=0 qbuf-free=32768 obl=18 oll=0 omem=0 events=rw cmd=exec

addr=192.168.0.100:4585 fd=6 name= age=4 idle=1 flags=N db=15 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=select
addr=192.168.0.100:4586 fd=7 name= age=4 idle=1 flags=N db=15 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=subscribe
addr=192.168.0.100:4587 fd=8 name= age=4 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
addr=192.168.0.100:4584 fd=9 name= age=1 idle=0 flags=x db=15 sub=0 psub=0 multi=1 qbuf=0 qbuf-free=32768 obl=18 oll=0 omem=0 events=rw cmd=exec

 17 ms
- watch_multi: 2 ms
- watch_transaction: 4 ms
- detect_buffers:
assert.js:92
  throw new assert.AssertionError({
        ^
AssertionError: true == false
    at process.<anonymous> (D:\pbase\node_redis\test.js:2066:12)
    at process.EventEmitter.emit (events.js:95:17)
    at process.exit (node.js:707:17)
    at process.<anonymous> (D:\pbase\node_redis\test.js:2061:13)
    at process.EventEmitter.emit (events.js:95:17)
    at process._fatalException (node.js:272:26)

huareal@gpx /cygdrive/d/pbase

分析test.js代碼中的2066行

process.on('exit', function (code) {
    assert.equal(true, connected);
    assert.equal(true, ended);//2066 line,這一行的assert應該是false
});

將example/simple.js複製到當前目錄

var PORT = 6379;
var HOST = '192.168.0.107';

var redis = require("./index"),
client = redis.createClient(PORT, HOST);

client.on("error", function (err) {
    console.log("error event - " + client.host + ":" + client.port + " - " + err);
});

client.set("string key", "string val", redis.print);
client.hset("hash key", "hashtest 1", "some value", redis.print);
client.hset(["hash key", "hashtest 2", "some other value"], redis.print);
client.hkeys("hash key", function (err, replies) {
    if (err) {
        return console.error("error response - " + err);
    }

    console.log(replies.length + " replies:");
    replies.forEach(function (reply, i) {
        console.log("    " + i + ": " + reply);
    });
});

client.quit(function (err, res) {
    console.log("Exiting from quit command.");
});

測試OK

複製monitor.js

var PORT = 6379;
var HOST = '192.168.0.107';
var redis = require("./index"),
    client = redis.createClient(PORT, HOST),
    util = require("util");

client.monitor(function (err, res) {
    console.log("Entering monitoring mode.");
});

client.on("monitor", function (time, args) {
    console.log(time + ": " + util.inspect(args));
});

運行啓動./node.exe monitor.js

$ ./node.exe monitor.js
Entering monitoring mode.
1377235416.197621: [ 'info' ]
1377235416.201388: [ 'set', 'string key', 'string val' ]
1377235416.201430: [ 'hset', 'hash key', 'hashtest 1', 'some value' ]
1377235416.201533: [ 'hset', 'hash key', 'hashtest 2', 'some other value' ]
1377235416.201562: [ 'hkeys', 'hash key' ]
運行./node.exe simple.js

$ ./node.exe simple.js
Reply: OK
Reply: 0
Reply: 0
2 replies:
    0: hashtest 1
    1: hashtest 2
Exiting from quit command.

更多實例分析可以從examples中測試。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章