Created
October 14, 2011 18:49
-
-
Save erickdavidson/1287957 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- private.setters = function setters(type, validate, key, value, lifetime, callback, cas){ | |
+ private.setters = function setters(type, validate, key, value, lifetime, compress, callback, cas){ | |
var flag = 0 | |
, memcached = this | |
, valuetype = typeof value | |
, length; | |
if (Buffer.isBuffer(value)){ | |
- flag = FLAG_BINARY; | |
+ if (compress) { | |
+ flag = FLAG_COMPRESSED; | |
+ value = new Buffer(zlib.deflate(value), 'binary'); | |
+ } else { | |
+ flag = FLAG_BINARY; | |
+ } | |
+ length = value.length; | |
value = value.toString('binary'); | |
} else if (valuetype !== 'string' && valuetype !== 'number'){ | |
flag = FLAG_JSON; | |
value = JSON.stringify(value); | |
+ length = Buffer.byteLength(value); | |
} else { | |
value = value.toString(); | |
+ length = Buffer.byteLength(value); | |
} | |
length = Buffer.byteLength(value); | |
if (length > memcached.maxValue) return private.errorResponse('The length of the value is greater than ' + memcached.maxValue, callback); | |
- memcached.command(function settersCommand(noreply){ return { | |
+ var lineBreakValue = new Buffer((new Buffer(value, 'binary')).length + 2); | |
+ lineBreakValue.write(LINEBREAK, 0, 'binary'); | |
+ lineBreakValue.write(value, 2, 'binary'); | |
+ | |
+ memcached.command(function settersCommand(noreply){ | |
+ | |
+ var commandBuffer = new Buffer([type, key, flag, lifetime, length].join(' ') + (cas ? ' ' + cas : '') + (noreply ? NOREPLY : ''), 'binary'); | |
+ var commandWithValue = new Buffer(commandBuffer.length + lineBreakValue.length); | |
+ commandBuffer.copy(commandWithValue, 0, 0); | |
+ lineBreakValue.copy(commandWithValue, commandBuffer.length, 0); | |
+ | |
+ return { | |
key: key | |
, callback: callback | |
, lifetime: lifetime | |
@@ -616,15 +643,11 @@ Client.config = { | |
, validate: validate | |
, type: type | |
, redundancyEnabled: true | |
- , command: [type, key, flag, lifetime, length].join(' ') + | |
- (cas ? ' ' + cas : '') + | |
- (noreply ? NOREPLY : '') + | |
- LINEBREAK + value | |
- }}); | |
+ , command: commandWithValue.toString('binary') }}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment