Skip to content

Commit

Permalink
Add support for touch
Browse files Browse the repository at this point in the history
  • Loading branch information
dterei committed Apr 29, 2016
1 parent d714e7b commit 13de9b5
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ can also implement a feature not a list if you think it would be good.

* Support flags
* Support multi commands
* Support touch
* Support CAS
* Consistent hashing for keys and/or pluggable hashing algorithm

Expand Down
33 changes: 33 additions & 0 deletions lib/memjs/memjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,39 @@ Client.prototype.prepend = function(key, value, callback) {
});
};

// TOUCH
//
// Touch sets an expiration value, given by _expires_, on the given _key_ in
// memcache. The operation only succeeds if the key is already present. The
// callback signature is:
//
// callback(err, success)
Client.prototype.touch = function(key, expires, callback) {
this.seq++;
var extras = makeExpiration(expires || this.options.expires);
var request = makeRequestBuffer(0x1C, key, extras, '', this.seq);

var logger = this.options.logger;
this.perform(key, request, function(err, response) {
if (err) {
if (callback) { callback(err, null); }
return;
}
switch (response.header.status) {
case 0:
if (callback) { callback(null, true); }
break;
case 1:
if (callback) { callback(null, false); }
break;
default:
var errorMessage = 'MemJS TOUCH: ' + errors[response.header.status];
logger.log(errorMessage);
if (callback) { callback(new Error(errorMessage), null); }
}
});
};

// FLUSH
//
// Flushes the cache on each connected server. The callback signature is:
Expand Down
42 changes: 42 additions & 0 deletions test/client_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,48 @@ test('PrependKeyDNE', function(t) {
});
});

test('TouchSuccessful', function(t) {
var n = 0;
var dummyServer = new MemJS.Server();
dummyServer.write = function(seq, requestBuf) {
var request = MemJS.Utils.parseMessage(requestBuf);
t.equal('hello', request.key.toString());
t.equal('', request.val.toString());
t.equal('\0\0\4\0', request.extras.toString());
n += 1;
dummyServer.respond({header: {status: 0, opaque: request.header.opaque}});
};

var client = new MemJS.Client([dummyServer]);
client.touch('hello', 1024, function(err, val) {
t.equal(null, err);
t.equal(true, val);
t.equal(1, n, 'Ensure touch is called');
t.end();
});
});

test('TouchKeyDNE', function(t) {
var n = 0;
var dummyServer = new MemJS.Server();
dummyServer.write = function(seq, requestBuf) {
var request = MemJS.Utils.parseMessage(requestBuf);
t.equal('hello', request.key.toString());
t.equal('', request.val.toString());
t.equal('\0\0\4\0', request.extras.toString());
n += 1;
dummyServer.respond({header: {status: 1, opaque: request.header.opaque}});
};

var client = new MemJS.Client([dummyServer]);
client.touch('hello', 1024, function(err, val) {
t.equal(null, err);
t.equal(false, val);
t.equal(1, n, 'Ensure ptouch is called');
t.end();
});
});

test('Failover', function(t) {
var n1 = 0;
var n2 = 0;
Expand Down

0 comments on commit 13de9b5

Please sign in to comment.