From fb7aa621f0cca8828ce360b40e55fb11464f3209 Mon Sep 17 00:00:00 2001 From: RichardSylvester Date: Tue, 16 Apr 2013 11:45:13 -0300 Subject: [PATCH] Update memcache.js -Added correct "cas" functionality (Check and Set) -Added "gets" command to be used in conjunction with the "cas" command as having cas without gets is pointless. --- lib/memcache.js | 67 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/lib/memcache.js b/lib/memcache.js index 8b826ad..0d19e2e 100644 --- a/lib/memcache.js +++ b/lib/memcache.js @@ -21,6 +21,14 @@ * * @author Tim Eggert * @license http://www.opensource.org/licenses/mit-license.html MIT License + * + * MODIFIED BY: + * Richard Sylvester - FXRLabs + * June 2012 + * + * Added correct "cas" functoinality" + * Added "gets" command to be used in conjunction with the "cas" command + * as having cas without gets is pointless. */ var tcp = require('net'), @@ -121,6 +129,11 @@ Client.prototype.get = function(key, callback) { return this.query('get ' + key, 'get', callback); }; +//RSS: Added June 7, 2012 +Client.prototype.gets = function(key, callback) +{ + return this.query('gets ' + key, 'gets', callback); +}; // 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); } @@ -224,6 +237,17 @@ Client.prototype.handle_received_data = function(){ var next_result_at = result[1]; var result_error = result[2]; + //RSS: Added June 7th, 2012 + var resultFlags = null; + var resultCasUnique = null; + + if(result.length >= 4) + resultFlags = result[3]; + + if(result.length >= 5) + resultCasUnique = result[4]; + //END RSS + // does the current message need more data than we have? // (this is how "get" ops ensure we've gotten all the data) if (next_result_at > this.buffer.length){ @@ -235,7 +259,10 @@ Client.prototype.handle_received_data = function(){ var callback = this.callbacks.shift(); if (callback != null && callback.fun){ this.replies++; - callback.fun(result_error, result_value); + //RSS: Modified June 7th, 2012 + //callback.fun(result_error, result_value); + callback.fun(result_error, result_value, resultFlags, resultCasUnique); + //END RSS } } }; @@ -288,6 +315,44 @@ Client.prototype.handle_get = function(buffer) { } }; +//RSS: Added June 7th, 2012 +Client.prototype.handle_gets = function(buffer) +{ + var next_result_at = 0; + var result_value = null; + var casUnique = null; + var flags = null; + var end_indicator_len = 3; + var result_len = 0; + + if (buffer.indexOf('END') == 0) + { + return [result_value, end_indicator_len + crlf_len, null, flags, casUnique]; + } else if (buffer.indexOf('VALUE') == 0 && buffer.indexOf('END') != -1) + { + first_line_len = buffer.indexOf(crlf) + crlf_len; + parts = buffer.substr(0,first_line_len - crlf_len).split(' '); + flags = parts[2]; + casUnique = parts.length >= 5 ? parts[4] : null; + + var end_indicator_start = buffer.indexOf('END'); + result_len = end_indicator_start - first_line_len - crlf_len; + result_value = buffer.substr(first_line_len, result_len); + + return [result_value, first_line_len + parseInt(result_len, 10) + crlf_len + end_indicator_len + crlf_len, null, flags, casUnique]; + } else { + var parts = buffer.substr(0,first_line_len - crlf_len).split(' '); + var first_line_len = buffer.indexOf(crlf) + crlf_len; + var result_len = parts[3]; + flags = parts[2]; + casUnique = parts.length >= 5 ? parts[4] : null; + result_value = buffer.substr(first_line_len, result_len); + + return [result_value, first_line_len + parseInt(result_len ) + crlf_len + end_indicator_len + crlf_len, null, flags, casUnique]; + } +}; +//END RSS + Client.prototype.handle_stats = function(buffer){ // special case - no stats at all