版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014267209/article/details/52217554
基本使用
开启服务(windows下): redis-server.exe redis.windows.conf
连接客户端: redis-cli.exe(默认地址和端口)
–redis-cli.exe -h 127.0.0.1 -p 6379
查看所有的键: keys *
插入一条: set test 1
获取一条:get test
node.js中的应用
var redis = require('redis');
var client = redis.createClient(6379,'localhost');
client.set('hello','This is a test nodeJS redis');
client.get('hello',function(err,v){
console.log('hello:',v);
});
需要安装redis模块 npm install redis
list: 不去重
rpush:右插入,rpush('testList','a');
lpush:左插入
rpop:右弹出,rpop('testList',function(err,v){...})
lpop:左弹出
lrange:取值,e.g:lrange(0,-1);
sets: 自动去重
sadd:添加元素,sadd('testSet',1);
smembers:查看元素,smembers('testSet',function(err,v){...});
消息中介
1.消息订阅
var client = require('./client');
client.subscribe('testPublish'); // 订阅一个频道
client.on('message',function(channel,msg){
console.log('client on message , channel:',channel, ' message:',msg);
});
2.消息发布
var client = require('./client');
client.publish('testPublish','message from pub.js');