如何将php数组存储到redis并在nodejs中检索?

时间:2017-06-16 09:24:10

标签: php node.js redis

我使用redis(predis / predis composer)在php中保存一些id,如下所示:

$redis = new Client();
$mailJson = $redis->get('mail');
$mail  = json_decode($mailJson);
$no = rand(100,500);
array_push($mail, $no);
$redis->set('mail', json_encode($mail));

我检索这个数组:

var redis = require("redis"),
    client = redis.createClient();
client.on('connect', function() {
    client.get('mail', function(err, mailIds) {
        console.log(mailIds);
    });
});

但是mailIds变量是字符串而不是像这样的数组:

[1,200,500,500,400,100,200,100]

是否可以访问mailIds项?

1 个答案:

答案 0 :(得分:3)

你需要解析json(一个字符串),以便在javascript / node.js中得到一个数组:

{"widget": {
    "debug": "on",
    "window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "width": 500,
        "height": 500
    },
    "image": { 
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": 250,
        "vOffset": 250,
        "alignment": "center"
    },
    "text": {
        "data": "Click Here",
        "size": 36,
        "style": "bold",
        "name": "text1",
        "hOffset": 250,
        "vOffset": 100,
        "alignment": "center",
        "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
    }
}}