-
-
Save thinkjson/e2ea2b81d5fa2716644fcd17b4e2d9fe to your computer and use it in GitHub Desktop.
Example of auto discovery for aws elasticache
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
| 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