在公司需要分享一下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中测试。