作者: RobanLee
(Code maker, PHP,JS,NODEJS,PYTHON, blog:http://www.robanlee.com)
Redis介绍(以下内容来自百度百科):
redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。
Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Python,Ruby,Erlang,PHP客户端,使用很方便。
准备工作:
使用NPM安装REDIS依赖包
依赖包地址: https://www.npmjs.org/package/redis
npm install redis
Robans-Pro:nodejs_serial_article robanlee$ npm install redis npm http GET https://registry.npmjs.org/redis npm http 200 https://registry.npmjs.org/redis [email protected] node_modules/redis Robans-Pro:nodejs_serial_article robanlee$ Robans-Pro:nodejs_serial_article robanlee$ ll total 0 drwxr-xr-x 3 robanlee wheel 102B 5 7 16:05 . drwxrwxrwt 12 root wheel 408B 5 7 16:05 .. drwxr-xr-x 3 robanlee staff 102B 5 7 16:05 node_modules Robans-Pro:nodejs_serial_article robanlee$ cd node_modules/ Robans-Pro:node_modules robanlee$ ll total 0 drwxr-xr-x 3 robanlee staff 102B 5 7 16:05 . drwxr-xr-x 3 robanlee wheel 102B 5 7 16:05 .. drwxr-xr-x 15 robanlee staff 510B 5 7 16:05 redis
在node_modules 目录下会多出一个redis目录,这里就是nodejs-redis所用到的包
启动REDIS SERVER
redis-server /usr/local/redis/redis.conf
代码之前的题外话
nodejs大部分操作都是异步操作,也就是需要有回调函数,那么回调函数就一定会有参数,在redis这个包里面,回调函数大部分是2个参数,一个是error,一个是响应的数据
例如:
client.info(function(err,response){ console.log(err,response); }); //如果这个查询没有错误,err的值是null
创建REDIS连接
以下代码展示创建一个redis socket连接,并且查询redis服务器信息,也就是 redis-cli INFO 命令
var redis = require("redis"); var client = redis.createClient(6379,'127.0.0.1'); client.info(function(err,response){ console.log(err,response); });
上面代码延伸出的一些问题:
1. 如果服务器没有连接上,会怎样?
2. 如果连接超时,怎么处理?
对于以上问题,我们需要对代码进行一些修改,以便更好地进行错误处理,这将是一个好的变成习惯.
createClient(port,host,options) 这个函数接受三个参数,第一个是连接端口,第二个是主机IP/名称,第三个参数则是配置项,KEY=>VALUE形式 例如: var client = redis.createClient(6379,'127.0.0.1',{connect_timeout:1}); //增加超时选项 错误处理: Nodejs 错误处理机制有2种,1种是同步操作时可以使用 try ... catch ..来捕获错误, 另外一种就是事件了, redisClient 有一个error 的事件,当出错的时候变回emmit 这个事件,使用方法: client.on('error',function(error){ console.log(error); });
REDIS GET & SET 操作:
//get & set client.set('roban', 'this is an testing val', function(err, response) { if (err) { console.log('Failed to set key of roban, error:' + err); return false; } client.get('roban',function(errGet,responseGet){ console.log('Val:'+responseGet); }); }); //以上代码输出: Robans-Pro:node robanlee$ node demo.js Val:this is an testing val
Set 操作
var sKey = 'roban:demo:sets'; var iMax = 100; function multiSet() { var val = Math.ceil(Math.random() * 10000); client.sadd(sKey, val, function(err, response) { if (err) { console.log('failed to push key to key:' + sKey); return false; } console.log('Success:val {' + val + '} has been pushed into key'); if (--iMax <= 0) { return false; } multiSet(); }); } multiSet(); //以上代码输出: Success:val {3489} has been pushed into key Success:val {9965} has been pushed into key Success:val {9631} has been pushed into key Success:val {1469} has been pushed into key Success:val {5782} has been pushed into key Success:val {1312} has been pushed into key Success:val {6067} has been pushed into key Success:val {1092} has been pushed into key Success:val {6901} has been pushed into key Success:val {4571} has been pushed into key Success:val {1807} has been pushed into key Success:val {9381} has been pushed into key Success:val {9968} has been pushed into key Success:val {7280} has been pushed into key Success:val {2790} has been pushed into key Success:val {1560} has been pushed into key Success:val {3788} has been pushed into key Success:val {9925} has been pushed into key Success:val {134} has been pushed into key Success:val {3871} has been pushed into key Success:val {753} has been pushed into key Success:val {3852} has been pushed into key Success:val {3092} has been pushed into key Success:val {1316} has been pushed into key Success:val {358} has been pushed into key Success:val {422} has been pushed into key Success:val {1215} has been pushed into key Success:val {1709} has been pushed into key Success:val {9093} has been pushed into key Success:val {8534} has been pushed into key Success:val {4487} has been pushed into key Success:val {8972} has been pushed into key Success:val {6842} has been pushed into key Success:val {9115} has been pushed into key Success:val {1713} has been pushed into key Success:val {6578} has been pushed into key ....
set类型相应的命令还有:
SADD SCARD SDIFF SDIFFSTORE SINTER SINTERSTORE SISMEMBER SMEMBERS SMOVE SPOP SRANDMEMBER SREM SSCAN SUNION SUNIONSTORE