Does Redis save strings as buffers on some OSs, and not on others?

I am using Redis 2.2.11 with Node on Ubuntu 11.10 and I save the string, but it returns 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); }); 

The following is created in the console:

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

Why does this happen as a string and exit as a buffer? The buffer makes debugging very difficult!

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

UPDATE: it also works fine without the encoding specified in CentOS 5.7. Is this something special for Ubuntu? Is there a system-wide fix?

+4
source share
2 answers

See: http://nodejs.org/docs/v0.3.1/api/buffers.html

Pure Javascript is Unicode friendly, but not good for binary data. when working with TCP streams or the file system, you need to process 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 like an array of integers, but allocation of raw memory to a V8 heap corresponds to raw memory. The buffer cannot be changed.

The buffer object is global.

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

Since you did not specify an encoding, by default it is displayed as raw data. You can use buffer.toString to create a standard JS string.

0
source

Since you did not specify an encoding, she does not know which encoding to use when printing it. Use the toString function with encoding as a parameter to register it correctly.

 client.hmget('user:' + id, "name", function(err, d) { console.log('data retrieved', d.toString('utf8')); }); 
0
source

Source: https://habr.com/ru/post/1383923/


All Articles