Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are using node-redis for saving the data then it will deal with Buffers directly so you can simply client.set(key, buff) or client.append(key, buff), so you don't need (want) to do any conversion.</p> <p>Node.js (simplified from Kevin)</p> <pre><code>var zlib = require('zlib'); var redis = require("redis"); var rc = redis.createClient(null, null, {detect_buffers: true}); // allow Buffers var message = new Buffer('My message'); zlib.deflate(message, function (err, deflated) { if (err) return console.error(err); rc.set("testkey", deflated, function (err, result) { if (err) return console.error(err); rc.quit(); }); }); </code></pre> <p>Ruby code (copied from Kevin above)</p> <pre><code>require 'zlib' require 'rubygems' require 'redis' redis = Redis.new def inflate(buffer) zstream = Zlib::Inflate.new buf = zstream.inflate(buffer) zstream.finish zstream.close buf end value = redis.get("testkey") puts inflate(value) </code></pre> <p>That works in retrieving the value properly, but changing the Node.js code to use <code>.toString('binary')</code> like you mention originally breaks the Ruby decoding like you said above.</p> <p>Here is an example to show that toString('binary') does mess with the data</p> <pre><code> console.log(deflated); console.log(new Buffer(deflated.toString('binary'))); </code></pre> <p>So I can't figure out what transformation Buffer.toString('binary') is doing since I believe it goes into the V8 Buffer code.</p> <p>But if you are still able to read it with Node, then you might want to extract it back out and save it the proper way without using the <code>.toString('binary')</code> just give the Buffer to the redis client set method and it will save it properly.</p> <p>Then it will be stored as binary and you can read it with ruby correctly using code like above.</p> <p>As for your node.js code, once you have it saving as binary properly (using Buffer directly in set call), then to retrieve it:</p> <pre><code>var rc = redis.createClient(null, null, {detect_buffers: true}); // allow Buffers rc.get(new Buffer(key), function (err, buff) { // use a Buffer for the key // buff is a Buffer now }); </code></pre> <p>By having detect_buffers turned on for node-redis, then when you pass a Buffer in as a key, then it will retrieve as a Buffer and won't convert.</p> <p>You could alternatively used <code>return_buffers = true</code> option, but I like <code>detect_buffers</code> so you can use the same client for both Buffer and non-Buffer data.</p> <p>PS. Make sure for your Ruby gem that you are using one of the latest versions, not an old one like 1.x (2.x added binary fixes).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload