/*——————————————————————————————
* 本文案例基於以下運行環境:
* 系統: CentOS 5.x
* NodeJS版本: 0.9 以上
* Redis版本: 2.8
* Redis-nodejs 擴展: 0.12.1
/*——————————————————————————————
Part 1: 安裝擴展
使用以下命令:
npm -g install redis
Redis實例:
Host: 127.0.0.1 Port: 6379
Part 2: 實例
var redis = require("redis"),
client = redis.createClient(6379,"127.0.0.1");
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(63719,'127.0.0.1',{connect_timeout:1}); //增加超時選項 錯誤處理: Nodejs 錯誤處理機制有2種,1種是同步操作時可以使用 try ... catch ..來捕獲錯誤, 另外一種就是事件了, redisClient 有一個error 的事件,當出錯的時候變回emmit 這個事件,使用方法: client.on('error',function(error){ console.log(error); });
Set
client.set(key,value,callback),callback 函數有2個回調參數,error和response, error表示操作過程中的錯誤提示值為null表示沒有錯誤,response為布爾值client.set("Roban","lee",function(err,response){
console.log(err,response);
});
Get
client.get(key,callback),callback 函數有2個回調參數,error和response, error表示操作過程中的錯誤提示值為null表示沒有錯誤,response為獲取到的值,null表示沒有獲取到數據client.get("Roban",function(err,response){
console.log(err,response); //will print lee
});
Hset
client.hset(hashkey,field,value,callback) 哈希數據類型, 第一個參數為KEY名稱,第二個為需要設置的字段KEY,第三個為值,第四個參數為回調參數,內容和set一致client.hset("roban:demo:hset","today","fine",function(err,response){
console.log(err,response);
});
Hmset
client.hmset(hashkey,field,value,field,value ….. callback) 哈希數據類型, 第一個參數為KEY名稱,后面的參數為不固定參數,數據格式是 key,value ,key, value client.hset("roban:demo:hset","lastday","notgood","nextday","willbefine",function(err,response){
console.log(err,response);
});
Hget
client.hget(hashkey,field,callback) 獲取hash數據中的某一個字段值client.hset("roban:demo:hset","today",function(err,response){
console.log(err,response);
});
Hgetall
client.hgetall(hashkey,callback) 獲取hash數據種所有的數據,包括字段與值client.hset("roban:demo:hset",function(err,response){
console.log(err,response);
});
BLpop
阻塞式彈出隊列數據,從數據頂部(左側)彈出,當 BLPOP 被調用時,如果給定 key 內至少有一個非空列表,那么彈出遇到的第一個非空列表的頭元素,並和被彈出元素所屬的列表的名字一起,組成結果返回給調用者。當存在多個給定 key 時, BLPOP 按給定 key 參數排列的先后順序,依次檢查各個列表。
語法:
BLPOP key [key ...] timeout
以下代碼表示,阻塞roban:demo:blpop這個隊列10秒鍾,如果有數據,立刻從左側彈出,如果沒有,持續阻塞,直到10秒
client.blpop("roban:demo:blpop",10,function(err,response){
console.log(err,response);
});
BRpop
阻塞式彈出隊列數據,從數據尾部(右側)彈出,當給定多個 key 參數時,按參數 key 的先后順序依次檢查各個列表,彈出第一個非空列表的尾部元素。使用方法同 BLPOP一致,只是數據彈出的方式不一樣
語法:
BRPOP key [key ...] timeout
client.brpop("roban:demo:blpop",10,function(err,response){
console.log(err,response);
});
SADD
將一個或多個 member 元素加入到集合 key 當中,已經存在於集合的 member 元素將被忽略。
假如 key 不存在,則創建一個只包含 member 元素作成員的集合。
當 key 不是集合類型時,返回一個錯誤。
語法:
SADD key member [member ...]
client.sadd("roban:demo:sdemo",hello,this,fuck,world,function(err,response){
console.log(err,response);
});
SCARD
返回集合 key 的基數(集合中元素的數量)。
語法:
SCARD key
client.scard("roban:demo:sdemo",function(err,response){
console.log("Number of key roban:demo:sdemo is:" + response);
});
SPOP
移除並返回集合中的一個隨機元素。
如果只想獲取一個隨機元素,但不想該元素從集合中被移除的話,可以使用 SRANDMEMBER 命令。
語法:
SPOP key
client.spop("roban:demo:sdemo",function(err,response){
console.log("Poped value of key roban:demo:sdemo is:" + response);
});
SRANDMEMBER
如果命令執行時,只提供了 key 參數,那么返回集合中的一個隨機元素。
從 Redis 2.6 版本開始, SRANDMEMBER 命令接受可選的 count 參數:
1. 如果 count 為正數,且小於集合基數,那么命令返回一個包含 count 個元素的數組,數組中的元素各不相同。如果 count 大於等於集合基數,那么返回整個集合。
2. 如果 count 為負數,那么命令返回一個數組,數組中的元素可能會重復出現多次,而數組的長度為 count 的絕對值。
該操作和 SPOP 相似,但 SPOP 將隨機元素從集合中移除並返回,而 SRANDMEMBER 則僅僅返回隨機元素,而不對集合進行任何改動。
語法:
SRANDMEMBER key [count]
client.srandmember("roban:demo:sdemo",function(err,response){
console.log("Poped value of key roban:demo:sdemo is:" + response);
});