參考: 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{});
(第三個(gè)參數(shù)callback為可選項(xiàng))
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: 鍵和值會(huì)被轉(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");
// 用法和上面一樣胚鸯,只是以鍵值對(duì)作為參數(shù)
HGET (hashKey, field, callback(err, reply))
// 單獨(dú)取hashMap的某個(gè)鍵
HSET (hashKey, field, val, callback(err, reply))
// 單獨(dú)設(shè)hashMap的某個(gè)鍵
// 新增鍵時(shí)reply為1刑桑,覆蓋舊鍵時(shí)reply為0
其他
Redis——set集合
Redis集合