在nodejs中从php获取memcache数据
Get memcache data from php in nodejs
我在 nodejs 中从内存缓存获取数据时遇到问题。
首先,我使用 php 在内存缓存中设置数据
Php代码:
$memcache = new Memcache;<br>
$memcache->connect("localhost",11211);<br>
$array = array("id"=>"1","name"=>"aaa");<br>
$memcache->set("key",$array);
在此之后,我检查是否在 memcache 中设置了此数据,它显示:
a:2:{s:2:"id";s:1:"1";s:4:"name";s:3:"aa a";}
一切似乎都还好。现在我想在 nodejs 中获取这些数据:
Nodejs 代码:
var memcache = require('mem');
var client = new memcache.Client(11211,'localhost');
client.connect();
client.get('key', function(err,result){
console.log(result);
});
这里我有问题...在控制台中我得到
a:2:{s:2:"id";s:1:"1";s:4:"name";s:3:"aa a";}
我的问题是
如何进入控制台名称:aaa?
我只补充一点:
console.log(result['name']); => show : undefined
请帮忙
PHP 数据看起来像是在为您自动序列化。
与其让这种情况发生,我建议您在插入内存缓存之前将数组编码为 JSON 数据,您可以使用 node 轻松对其进行解码。
所以
$memcache->set("key",$array);
做:
$memcache->set("key", json_encode($array));
然后在节点中,而不是:
client.get('key', function(err,result){
console.log(result);
});
做:
client.get('key', function(err,result){
var object = JSON.parse(result);
console.log(object);
console.log(object['name']);
});
这会将数据加载到 JS 对象中。
尝试使用更便携的格式在系统之间共享数据。例如。 JSON.
$memcache = new Memcache;
$memcache->connect("localhost",11211);
$data = json_encode(array("id"=>"1","name"=>"aaa"));
$memcache->set("key",$data);
我希望这会在内存缓存中显示为
{ "id" : 1, "name": "aaa" }
那么你应该可以将其提取为 JSON:
var memcache = require('mem');
var client = new memcache.Client(11211,'localhost');
client.connect();
client.get('key', function(err,result){
console.log(result);
var entry = JSON.parse(result);
console.log(entry.name);
});
我在 nodejs 中从内存缓存获取数据时遇到问题。
首先,我使用 php 在内存缓存中设置数据
Php代码:
$memcache = new Memcache;<br>
$memcache->connect("localhost",11211);<br>
$array = array("id"=>"1","name"=>"aaa");<br>
$memcache->set("key",$array);
在此之后,我检查是否在 memcache 中设置了此数据,它显示:
a:2:{s:2:"id";s:1:"1";s:4:"name";s:3:"aa a";}
一切似乎都还好。现在我想在 nodejs 中获取这些数据:
Nodejs 代码:
var memcache = require('mem');
var client = new memcache.Client(11211,'localhost');
client.connect();
client.get('key', function(err,result){
console.log(result);
});
这里我有问题...在控制台中我得到
a:2:{s:2:"id";s:1:"1";s:4:"name";s:3:"aa a";}
我的问题是 如何进入控制台名称:aaa? 我只补充一点:
console.log(result['name']); => show : undefined
请帮忙
PHP 数据看起来像是在为您自动序列化。
与其让这种情况发生,我建议您在插入内存缓存之前将数组编码为 JSON 数据,您可以使用 node 轻松对其进行解码。
所以
$memcache->set("key",$array);
做:
$memcache->set("key", json_encode($array));
然后在节点中,而不是:
client.get('key', function(err,result){
console.log(result);
});
做:
client.get('key', function(err,result){
var object = JSON.parse(result);
console.log(object);
console.log(object['name']);
});
这会将数据加载到 JS 对象中。
尝试使用更便携的格式在系统之间共享数据。例如。 JSON.
$memcache = new Memcache;
$memcache->connect("localhost",11211);
$data = json_encode(array("id"=>"1","name"=>"aaa"));
$memcache->set("key",$data);
我希望这会在内存缓存中显示为
{ "id" : 1, "name": "aaa" }
那么你应该可以将其提取为 JSON:
var memcache = require('mem');
var client = new memcache.Client(11211,'localhost');
client.connect();
client.get('key', function(err,result){
console.log(result);
var entry = JSON.parse(result);
console.log(entry.name);
});