Node.js Redis API

2016-03-21  本文已影响2758人  WTIFS

参考: 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{});
(第三个参数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: 键和值会被转为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");
// 用法和上面一样,只是以键值对作为参数

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

// 单独取hashMap的某个键

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

// 单独设hashMap的某个键
// 新增键时reply为1,覆盖旧键时reply为0

其他

Redis——set集合
Redis集合

上一篇下一篇

猜你喜欢

热点阅读