Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decode/decompress values from memcached-backed Rails cache (Dalli gem) in Node.js

I have a Rails application that is caching data in memcached via the Dalli gem (https://github.com/mperham/dalli).

I would like to read the contents of this cache from Node.js. I am using the mc module to interface with memcached in Node.js.

The problem I'm running into is with encoding and compression. Dalli uses Zlib::Deflate.deflate(data) (https://github.com/mperham/dalli/blob/master/lib/dalli/compressor.rb). When I attempt to inflate from Node.js I get an error when trying to inflate with the zlib module:

{ [Error: incorrect header check] errno: -3, code: 'Z_DATA_ERROR' }

Here is relevant Ruby/Rails code:

config.cache_store = :dalli_store, memcached_server, {compress: true}

And the relevant Node.js code:

client = new Memcached.Client(MEMCACHED_HOSTNAME, Memcached.Adapter.raw);


client.get(key, function (err, response) {
  var data = response[key];

  zlib.inflate(data.buffer, function (err, buf) {
    console.log(err, buf);
  });
});

The buffer the comes back from memcached's string value looks like this:

'\u0004\b[\u0015i\u0006i\u0007i\bi\ti\ni\u000bi\fi\ri\u000ei\u000fi\u0010i\u0011i\u0012i\u0014i\u0015i\u0016'

The value that I'm expecting after inflating is something like: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17]

like image 521
Ian Christian Myers Avatar asked Apr 08 '14 17:04

Ian Christian Myers


1 Answers

Important detail not mentioned above: dalli by default uses ruby native marshalling to serialize the values.

If you want to use the memcached values from multiple languages, consider using a different serializer, see the documentation on Configuration https://github.com/petergoldstein/dalli#configuration

like image 94
rud Avatar answered Nov 05 '22 06:11

rud