list)});client.sa……" />
cnpminstallredis新建redis.js,引入redis
varredis=require('redis');varclient=redis.createClient(6379,'127.0.0.1');字符串操作
client.set('hello','thisishello')client.get('hello',(err,v)=>{ console.log(err,v)})Hash操作
client.hset("hash","today","fine",function(err,response){ console.log(err,response);});client.hset("hash","tomorow","notfine",function(err,response){ console.log(err,response);});client.hget("hash","today",function(err,response){ console.log(err,response);});client.hgetall("hash",function(err,response){ console.log(err,response);});列表操作
假设文件名为list.js,重复执行Node list.js命令,会重复向testLists内插入a,b,c。这是列表和集合的区别之一。
client.rpush('Lists','a');//尾(右边)部插入client.rpush('Lists','b');client.lpush('Lists','c');//头(左边)部插入client.lpop('Lists',(err,list)=>{//左边弹出(删除) console.log(err,list)});client.rpop('Lists',(err,list)=>{//右边弹出(删除) console.log(err,list)});client.lrange('Lists',0,-1,(err,list)=>{//输入testLists变量,0和-1是下标,代表全部内容 console.log(err,list)});集合操作
如果Set内部已经有1,2,3,重复执行此文件不会重复插入。
client.sadd('Set',1)client.sadd('Set',2)client.sadd('Set',3)client.smembers('Set',(err,v)=>{ console.log(err,v)})
判断是否重复
client.sismember('Set',19,function(err,reply){});消息订阅
新建推送文件pub.js
client.publish('Publish','messageformpub.js')
新建订阅文件sub.js 订阅Publish频道,使用on()方法监听message
client.subscribe('Publish');client.on('message',(channel,msg)=>{ console.log(channel,msg)})
先执行node sub.js执行订阅操作 再执行node pub.js推送消息message form pub.js, 就可以在sub,js的输入栏中看到打印:
testPublishmessageformpub.js
有话要说...