Skip to content

Commit

Permalink
implemented all of the store operations. updated the readme docs
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcal committed Aug 27, 2010
1 parent 779b0bc commit ac76422
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 20 deletions.
36 changes: 24 additions & 12 deletions README.md
Expand Up @@ -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?
Expand All @@ -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.

Expand Down
42 changes: 34 additions & 8 deletions lib/memcache.js
Expand Up @@ -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);
};
Expand Down

0 comments on commit ac76422

Please sign in to comment.