Created
December 21, 2016 08:55
-
-
Save CaptainJiNX/8137987a7d67a5a55a0168e205d7fd3c to your computer and use it in GitHub Desktop.
Example of auto discovery for aws elasticache
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
const Memcached = require('memcached'); | |
const configClient = new Memcached('xyz123.cfg.cache.amazonaws.com:11211'); | |
configClient.command(configGetCluster); | |
function configGetCluster() { | |
return { | |
command: 'config get cluster', | |
callback: handleClusterResponse | |
}; | |
} | |
function handleClusterResponse(err, response) { | |
const cacheNodes = getNodes(response); | |
console.log('Discovered nodes: ', cacheNodes); | |
// ... Do stuff ... | |
const client = new Memcached(cacheNodes); | |
client.set('qwerty', 'blahonga', 60, (err) => { | |
client.get('qwerty', (err, data) => { | |
console.log('Response: ', err || data); | |
}); | |
}); | |
// ... | |
configClient.end(); | |
} | |
function getNodes(response) { | |
function parseNode(node) { | |
const parts = node.split('|'); | |
const hostName = parts[0]; | |
const port = parts[2]; | |
return `${hostName}:${port}`; | |
} | |
const lines = response.split(/\r?\n/); | |
const unparsedNodes = lines[1].split(' '); | |
return unparsedNodes.map(parseNode); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment