elbart / node-memcache

node.js memcached client
285 stars 64 forks source link

Multiple key get query #21

Open schloerke opened 12 years ago

schloerke commented 12 years ago

Memcached supports multiple keys in a get query. I added a handler to support this feature and thought you would find it useful.

I changed the exports.get method to accomodate arrays or strings with spaces. The handler for the multiple key get followed the same pattern as the regular get handler.

The major difference, to me, is that I am specifically looking for the end tag position and not trying to figure it out according to the length given. This would not be fun to calculate for a multiple key get query. The crlf value is present in all the responses. I used the crlf as something to key on.

Any questions or places that I can fill in, let me know!

Best, Barret

Ps. Thank you for making this a simple to follow node module. I appreciate it.

elbart commented 12 years ago

Hi Barret,

thank you very much for your contribution. I will find some time to review this by the end of the week. If you could write some tests for the multi_get query would be awesome. Otherwise I would try to do it at the weekend, too.

Regards,

Tim

schloerke commented 12 years ago

I am having trouble running the test suit itself (pointing it to my external box). I can't connect to my external box. I can telnet to it, I can connect to it in my personal server, but I can't run the test suite.... :-S

So, I'm just going to paste it below.

exports['multi_get'] = function(beforeExit) {
  var n = 0;

  mc.set('multi_A', 'A', function(err, response) {
    assert.equal(response, 'STORED');
    n++;
    mc.set('multi_B', 'B', function(err, response) {
      assert.equal(response, 'STORED');
      n++;
      mc.set('multi_C', 'C', function(err, response) {
        assert.equal(response, 'STORED');
        n++;

        var expectedOutput = {multi_A: 'A', multi_B: 'B', multi_C: 'C'};

        // allow for string with spaces
        mc.get('A B C', function(err, response) {
          n++;
          assert.equal(response, expectedOutput);
        });

        // allow for array of items
        mc.get(['A', 'B', 'C'], function(err, response) {
          n++;
          assert.equal(response, expectedOutput);
        });

        // return only values that exist
        mc.get(['A', 'B', 'C', '_no_exist_key'], function(err, response) {
          n++;
          assert.equal(response, expectedOutput);
        });

        // if an array is given, return a {} at a minimum
        mc.get(['does_not_exist_A', 'does_not_exist_B'], function(err, response) {
          n++;
          assert.equal(response, {});
        });

        // memcached server should return an error
        mx.get([], function(err, response) {
          n++;
          assert.equal(error, 'ERROR');
        });

        // memcached server should return an error
        mx.get('', function(err, response) {
          n++;
          assert.equal(error, 'ERROR');
        });
      }); 
    });
  });

  beforeExit(function() {
    assert.equal(9, n);
  });

};