Node.js Redis API

參考: Redis client library

Usage

var redis = require("redis"),
client = redis.createClient();

// if you'd like to select database 3, instead of 0 (default),
// call client.select(3, function() { /* ... */ }); 

client.on("error", function (err) {
    console.log("Error " + err);
});

client.set("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();
});

GET (key, callback)

client.get("key", function (err, value){
    if (err) throw(err)
    // value is null when the key is missing 
    console.log(value)
})

SET (key, value, (callback))

client.set("some key", "some val", function(err, reply){});
client.set(["some other key", "some val"], function(err, reply{});
(第三個參數(shù)callback為可選項)

HGETALL (hashKey, callback)

Example:
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' }

HMSET

HMSET (hashKey, obj, (callback))

 client.HMSET(key, {
    "0123456789": "abcdefghij", // NOTE: 鍵和值會被轉(zhuǎn)為string
    "some manner of key": "a type of value"
});

HMSET (hashKey, fieldl1, val1, ...fieldN, valN, (callback))

client.HMSET(key1, "0123456789", "abcdefghij", "some manner of key", "a type of value");
// 用法和上面一樣,只是以鍵值對作為參數(shù)

HGET (hashKey, field, callback(err, reply))

// 單獨取hashMap的某個鍵

HSET (hashKey, field, val, callback(err, reply))

// 單獨設(shè)hashMap的某個鍵
// 新增鍵時reply為1,覆蓋舊鍵時reply為0

其他

Redis——set集合
Redis集合

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容