Created
May 25, 2016 07:59
-
-
Save rtm/bf37b45dd246c82a3d4cd650d9815c3c to your computer and use it in GitHub Desktop.
chunked version of memcached
This file contains hidden or 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
// Wrap memcached to chunkify long values ("blobs"). | |
// Write individual chunks using a key constructed from a unique GUID and chunk number. | |
// Store the GUID and the number of chunks as the value of the underlying key. | |
// | |
// We assume that memcached.read and memcached.write are asynchronous and return promises. | |
// We also define asynchonous assemble and disassemble routes. | |
// | |
// The behavior of this code is that when a key is read, | |
// even if another client starts writing a new value for the | |
// key, the previous value will be returned. | |
function makeChunkKey(guid, chunk_num) { return guid + '@' + chunk_num; } | |
function set(key, blob) { | |
var guid = getGuid(), i = 0; | |
return disassemble(blob) . | |
then(chunks => | |
Promise.all(chunks . map(chunk => memcached.write(makeChunkKey(guid, i++), chunk))) | |
) . | |
then(() => memcached.write(key, guid + ':' + i)); | |
} | |
function get(key) { | |
return memcached.read(key) . | |
then(blob_info => blob_info.split(':')) . | |
then(([guid, chunk_num]) => | |
Promise.all( | |
new Array(chunk_num) . fill() . map((e, i) => | |
memcached.read(makeChunkKey(guid, i))) | |
)) . | |
then(assemble); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment