node.js 之 Redis
2016-07-21 00:40
423 查看
redis数据库
1、 Redis是一个key-value类型的内存数据库,而key全部都是 字符串 、哈希表(map)、列表(list)、集合(set)、有序集。2、 Redis是通过MULTI/DISCARD/EXEC/WATCH这4个命令来实现事务功能。事务提供了一种“将多个命令打包,然后一次性、按顺序执行”的机制,在事务完成之前,客户端的其他命令都是阻塞状态。
node 使用redis
安装redis 的客户端
npm install redis
var redis = require(redis), client = redis.createClient(port, host, opt); //opt 是选项的按钮 为了安全可以设置密码 //默认连接的是第一个数据库, 如果你想连接第三个服务器 实例: // client.select(3, function() { /* ... */ }); client.on(error, function (err) { console.log(err.message); }); clinet.on(ready, function(err){ console.log("ready"); }); //client.on 是用来用来监听事件的, 当与redis服务器连接成功后会 //触发这个事件,此时表示已经准备好接收命令,当这个事件触发之 //前client命令会存在队列中,当一切准备就绪后按顺序调用
如上这些接口都是异步的
每个Redis命令暴露在客户对象的功能。所有的功能以一个args数组加上可选的回调函数或变量数的各个参数后跟一个可选的回调。
//单值的设置和获取 client.set(string key, string val, redis.print); client.get(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) { console.log(replies.length + replies:); replies.forEach(function (reply, i) { console.log( + i + : + reply); }); client.quit(); });
client.hmset("hosts", "mjr", "1", "another", "23", "home", "1234"); client.hgetall("hosts", function (err, obj) { console.dir(obj); }); Output: { mjr: '1', another: '23', home: '1234' }
其中有些回调函数是可选的, 其实有时候 统一, 就都写上吧, 也不费劲~~
订阅和发布功能
这个程序打开两个客户端连接,支持其中的一个频道,并公布在其他渠道var redis = require("redis"); var sub = redis.createClient() , pub = redis.createClient(); var msg_count = 0; sub.on("subscribe", function (channel, count) { pub.publish("a nice channel", "I am sending a message."); pub.publish("a nice channel", "I am sending a second message."); pub.publish("a nice channel", "I am sending my last message."); }); sub.on("message", function (channel, message) { console.log("sub channel " + channel + ": " + message); msg_count += 1; if (msg_count === 3) { sub.unsubscribe(); sub.quit(); pub.quit(); } }); sub.subscribe("a nice channel");
订阅的 事件
事务
MULTI命令排队等待直到EXEC 发出,然后所有的命令都是原子运行的redis。在node_redis接口是通过调用客户端返回一个单独的多目标。对于multi()如果任何命令不能队列,所有的命令都回滚,没有一个将被执行var redis = require("./index"), client = redis.createClient(), set_size = 20; client.sadd("bigset", "a member"); client.sadd("bigset", "another member"); while (set_size > 0) { client.sadd("bigset", "member " + set_size); set_size -= 1; } // multi chain with an individual callback client.multi() .scard("bigset") .smembers("bigset") .keys("*", function (err, replies) { // NOTE: code in this callback is NOT atomic // this only happens after the the .exec call finishes. client.mget(replies, redis.print); }) .dbsize() .exec(function (err, replies) { console.log("MULTI got " + replies.length + " replies"); replies.forEach(function (reply, index) { console.log("Reply " + index + ": " + reply.toString()); }); });
除了将命令单独添加到多个队列之外,还可以将一个命令和参数数组传递给构造函数
var redis = require("redis"), client = redis.createClient(), multi; client.multi([ ["mget", "multifoo", "multibar", redis.print], ["incr", "multifoo"], ["incr", "multibar"] ]).exec(function (err, replies) { console.log(replies); });
拆分命令队列, 直到multi.exec 被调用。
var redis = require("redis"), client = redis.createClient(), multi; // start a separate multi command queue multi = client.multi(); multi.incr("incr thing", redis.print); multi.incr("incr other thing", redis.print); // runs immediately client.mset("incr thing", 100, "incr other thing", 1, redis.print); // drains multi queue and runs atomically multi.exec(function (err, replies) { console.log(replies); // 101, 2 });
Multi-word commands
client.script('load', 'return 1'); client.multi().script('load', 'return 1').exec(...); client.multi([['script', 'load', 'return 1']]).exec(...);
redis 这个博客写的不错 :
http://github.thinkingbar.com/redisbook_chapter01/
推荐本书
《redis设计与实现》
参考:
https://github.com/NodeRedis/node_redis
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- 使用 HTTP 上传 G 级的文件之 Node.js 版本
- redis安装问题小结
- mongo实现消息队列
- 数据库链接字符串查询网站
- Node.js压缩web项目中的js,css和图片
- node连接mysql数据库
- DB2实例管理
- DB2实例管理
- 使用 Node.js 构建交互式命令行工具
- 保障MySQL数据安全的14个最佳方法
- mysql问答汇集
- async.js 学习记录
- 异步流程控制:7 行代码学会 co 模块
- 新时代编辑神器:Atom
- rem : web app适配的秘密武器
- jquery高级应用之Deferred对象
- 使用 Redis 和 Python 构建一个共享单车的应用程序
- 第三章 数据库备份和还原