Skip to content

Instantly share code, notes, and snippets.

@CaptainJiNX
Created December 21, 2016 08:55
Show Gist options
  • Save CaptainJiNX/8137987a7d67a5a55a0168e205d7fd3c to your computer and use it in GitHub Desktop.
Save CaptainJiNX/8137987a7d67a5a55a0168e205d7fd3c to your computer and use it in GitHub Desktop.
Example of auto discovery for aws elasticache
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