Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Memcache in node.js is returning object with varying size

Memcache in node.js is returning object with varying size


Long-time reader, first-time poster.

I am using node v0.6.6 on OS X 10.7. I have not yet tried this in any other environment. I am using this client:

When I use the following code, data randomly contains a few more bytes (as reported by console.log()), which leads to this image: (and many other JPG do this). favicon seems OK and HTML/CSS/javascript all work.

In other words: if I request the image, ~70% of the time the image is returned correctly; the other 30% - data reports a few more bytes and the image appears corrupt in the browser.

client.get(key, function(err, data) {
    if (err) throw err;
    if (data) {
        res.writeHead(200, {'Content-Type': type, 'Content-Length': data.length});
        console.log('Sending with length: ' + data.length);
        res.end(data, 'binary');

I have been messing with this for several hours and I can honestly say I am stumped. I am hoping someone can show me the error in my ways. I tried searching if there was a way to properly store Binary data with memcache but there's no relevant information.

Extra information: it happens with various JPG images; all images are around 100-300KB or less in filesize. For example, one image has reported the following sizes: 286442, 286443, 286441. This problem DOES NOT occur if I straight read data from disk and serve it with node.

Thanks in advance.

Edit I updated my node version and issue persists. Actual test source photo and corrupt photo can be found in my comment below (stackoverflow doesn't permit more links).

Problem courtesy of: joneszach


Elbart's node-memcache does not Handle Binary Values correctly for the reasons Steve Campbell suggests: node-memcache does not give the client direct access to the buffer. By stringifying the buffers, binary data is corrupted.

Use the 'mc' npm. ( npm install mc )

Caveat: I'm the author of the 'mc' npm. I wrote it specifically to handle Binary Values over memcache's text protocol.

Solution courtesy of: bluejack


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Memcache in node.js is returning object with varying size


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription