我在 Ubuntu 11.10 上使用 Redis 2.2.11 和 Node,我正在保存一个字符串,但它作为缓冲区返回。
id = 1234;
console.log('data', data);
client.hmset("user:" + id, "name", data['name'] );
client.hmget('user:' + id, "name", function(err, d) {
console.log('data retrieved', d);
});
这会在控制台产生以下内容:
data { name: 'RealServer' }
data retrieved [ <Buffer 41 6e 6e 61 52 65 61 6c 53 65 72 76 65 72> ]
为什么它以字符串的形式进入,以缓冲区的形式出来? 缓冲区使调试变得非常困难!
在我的本地设置(带有 Redis 2.2.14 的 MacOS 10.6)中,检索到的数据打印为字符串,很好。我想找到一个可以在两个系统上继续工作的解决方案。
更新:即使没有在 CentOS 5.7 上指定的编码,它也能正常工作。这是 Ubuntu 特有的东西吗?是否有系统范围的修复?
最佳答案
参见:http://nodejs.org/docs/v0.3.1/api/buffers.html
Pure Javascript is Unicode friendly but not nice to binary data. When dealing with TCP streams or the file system, it's necessary to handle octet streams. Node has several strategies for manipulating, creating, and consuming octet streams.
Raw data is stored in instances of the Buffer class. A Buffer is similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap. A Buffer cannot be resized.
The Buffer object is global.
Converting between Buffers and JavaScript string objects requires an explicit encoding method.
因为您没有指定编码,所以它默认显示为原始数据。您可以使用 buffer.toString
来生成标准的 JS 字符串。
关于javascript - Redis 在某些操作系统上将字符串保存为缓冲区,而不是其他操作系统?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8329252/