Redis将字符串保存为某些操作系统(而不是其他操作系统)上的缓冲区吗? [英] Redis saving strings as buffers on some OSs, not others?

查看:107
本文介绍了Redis将字符串保存为某些操作系统(而不是其他操作系统)上的缓冲区吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ubuntu 11.10上将Redis 2.2.11与Node一起使用,并且我保存了一个字符串,但该字符串作为缓冲区返回.

I'm using Redis 2.2.11 with Node on Ubuntu 11.10, and I'm saving a string but it's being returned as a Buffer.

    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);
    });

这将在控制台上产生以下内容:

This produces the following at the console:

data { name: 'RealServer' }
data retrieved [ <Buffer 41 6e 6e 61 52 65 61 6c 53 65 72 76 65 72> ]

为什么它以字符串形式进入,而以Buffer形式出现呢? 缓冲区使调试非常困难!

Why is it going in as a string, and coming out as a Buffer? The Buffer makes debugging very difficult!

在我的本地设置(带有Redis 2.2.14的MacOS 10.6)上,检索到的数据打印为字符串,就好了.我想找到一个在两个系统上都可以继续使用的解决方案.

On my local setup (MacOS 10.6 with Redis 2.2.14) the data retrieved prints as a string, just fine. I'd like to find a solution that continues to work on both systems.

更新:如果没有在CentOS 5.7上指定编码,它也可以正常工作.这是Ubuntu特有的吗?是否有系统范围的修复程序?

UPDATE: It also works fine without an encoding specified on CentOS 5.7. Is this something specific to Ubuntu? Is there a system-wide fix?

推荐答案

请参阅: http://nodejs.org/docs/v0.3.1/api/buffers.html

纯Javascript对Unicode友好,但对二进制数据不利.什么时候 处理TCP流或文件系统,有必要处理 八位位组流.节点有几种策略可用于操作,创建, 和消耗八位位组流.

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.

原始数据存储在Buffer类的实例中.缓冲区是 类似于整数数组,但对应于原始内存 在V8堆之外分配.缓冲区的大小无法调整.

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.

Buffer对象是全局的.

The Buffer object is global.

在缓冲区和JavaScript字符串对象之间进行转换需要一个 显式编码方法.

Converting between Buffers and JavaScript string objects requires an explicit encoding method.

由于未指定编码,因此默认情况下它将显示为原始数据.您可以使用buffer.toString生成标准的JS字符串.

Because you've not specified an encoding, it displays as raw data by default. You can use buffer.toString to produce a standard JS string.

这篇关于Redis将字符串保存为某些操作系统(而不是其他操作系统)上的缓冲区吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆