1. 程式人生 > >node.js下redis簡單操作例項

node.js下redis簡單操作例項

from:http://cnodejs.org/topic/5200755c44e76d216a1620df

npm install redis
// redis 連結var redis   = require('redis');var client  = redis.createClient('6379','127.0.0.1');
// redis 連結錯誤
client.on("error",function(error){
    console.log(error);});
// redis 驗證 (reids.conf未開啟驗證,此項可不需要)
client.auth("foobared");
client
.select('15',function(error){if(error){ console.log(error);}else{// set client.set('str_key_0','0',function(error, res){if(error){ console.log(error);}else{ console.log(res);}// 關閉連結 client.end();});}});
client.select('15',function(error){if(error
){ console.log(error);}else{// get client.get('str_key_0',function(error, res){if(error){ console.log(error);}else{ console.log(res);}// 關閉連結 client.end();});}});
client.select('15',function(error){if(error){
        console.log(error);}else{// hmset
var info ={}; info.baidu ='www.baidu.com'; info.sina ='www.sina.com'; info.qq ='www.qq.com'; client.hmset('site', info,function(error, res){if(error){ console.log(error);}else{ console.log(res);}// 關閉連結 client.end();});}});
client.select('15',function(error){if(error){
        console.log(error);}else{// hmget
        client.hmget('site','baidu',function(error, res){if(error){
                console.log(error);}else{
                console.log(res);}// 關閉連結
            client.end();});}});
client.select('15',function(error){if(error){
        console.log(error);}else{// hgetall
        client.hgetall('site',function(error, res){if(error){
                console.log(error);}else{
                console.log(res);}// 關閉連結
            client.end();});}});
client.select('15',function(error){if(error){
        console.log(error);}else{// lpush
        client.lpush('list','key_0');
        client.lpush('list','key_1');
        client.end();}});
client.select('15',function(error){if(error){
        console.log(error);}else{// lrange
        client.lrange('list','0','-1',function(error, res){if(error){
                console.log(error);}else{
                console.log(res);}// 關閉連結
            client.end();});}});