From ac764226afba210e9264e106cbfd3f7b92e2e40a Mon Sep 17 00:00:00 2001 From: Cal Henderson Date: Fri, 27 Aug 2010 18:31:04 -0500 Subject: [PATCH] implemented all of the store operations. updated the readme docs --- README.md | 36 ++++++++++++++++++++++++------------ lib/memcache.js | 42 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 58 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 157247b..5c1e59b 100644 --- a/README.md +++ b/README.md @@ -43,20 +43,22 @@ The Client object emits 4 important events - connect, close, timeout and error. After connecting, you can start to make requests. - client.get('key', function(response){ - - }); + client.get('key', function(result, error){ + + // all of the callbacks have two arguments. + // 'result' may contain things which aren't great, but + // aren't really errors, like 'NOT_STORED' - client.set('key', 'value', function(response){ + }); - // response will be 'STORED' if it worked, - // else an error (huh?) + client.set('key', 'value', function(success, error){ - // lifetime argument is optional + // lifetime is optional. the default is + // to never expire (0) }, lifetime); - client.del('key', function(response){ + client.del('key', function(success, error){ // delete a key from cache. // response? @@ -67,10 +69,20 @@ After connecting, you can start to make requests. // response contains server version? }); - client.increment('key', value, function(response){ - }); - client.decrement('key', value, function(response){ - }); + + // all of the different "store" operations + // (lifetime & flags are both optional) + client.set(key, value, callback, lifetime, flags); + client.add(key, value, callback, lifetime, flags); + client.replace(key, value, callback, lifetime, flags); + client.append(key, value, callback, lifetime, flags); + client.prepend(key, value, callback, lifetime, flags); + client.cas(key, value, unique, callback, lifetime, flags); + + // increment and decrement + // (value is optional, defaults to 1) + client.increment('key', value, callback); + client.decrement('key', value, callback); Once you're done, close the connection. diff --git a/lib/memcache.js b/lib/memcache.js index 43efc1c..25a98ee 100644 --- a/lib/memcache.js +++ b/lib/memcache.js @@ -105,19 +105,45 @@ Client.prototype.get = function(key, callback) { return this.query('get ' + key, 'get', callback); }; -Client.prototype.set = function(key, value, callback, lifetime) { - if (typeof(callback) != 'function') { - lifetime = callback; - callback = null; - } - + +// all of these store ops (everything bu "cas") have the same format +Client.prototype.set = function(key, value, callback, lifetime, flags) { return this.store('set', key, value, callback, lifetime, flags); } +Client.prototype.add = function(key, value, callback, lifetime, flags) { return this.store('add', key, value, callback, lifetime, flags); } +Client.prototype.replace = function(key, value, callback, lifetime, flags) { return this.store('replace', key, value, callback, lifetime, flags); } +Client.prototype.append = function(key, value, callback, lifetime, flags) { return this.store('append', key, value, callback, lifetime, flags); } +Client.prototype.prepend = function(key, value, callback, lifetime, flags) { return this.store('prepend', key, value, callback, lifetime, flags); } +Client.prototype.store = function(cmd, key, value, callback, lifetime, flags) { + + if (typeof(callback) != 'function') { + lifetime = callback; + callback = null; + } + + var set_flags = flags || 0; + var exp_time = lifetime || 0; + var value_len = value.length || 0; + var query = [cmd, key, set_flags, exp_time, value_len]; + + return this.query(query.join(' ') + crlf + value, 'set', callback); +}; + +// "cas" is a store op that takes an extra "unique" argument +Client.prototype.cas = function(key, value, unique, callback, lifetime, flags) { + + if (typeof(callback) != 'function') { + lifetime = callback; + callback = null; + } + + var set_flags = flags || 0; var exp_time = lifetime || 0; var value_len = value.length || 0; - var query = 'set ' + key + ' 0 ' + exp_time + ' ' + value_len + crlf + value; + var query = ['cas', key, set_flags, exp_time, value_len, unique]; - return this.query(query, 'set', callback); + return this.query(query.join(' ') + crlf + value, 'set', callback); }; + Client.prototype.del = function(key, callback) { return this.query('delete ' + key, 'delete', callback); };