Skip to content

Instantly share code, notes, and snippets.

@stockholmux
Last active June 19, 2020 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stockholmux/b225524ddd03ae50b4089a8f509ce1a2 to your computer and use it in GitHub Desktop.
Save stockholmux/b225524ddd03ae50b4089a8f509ce1a2 to your computer and use it in GitHub Desktop.
node_redis_connection
let
redis = require('redis'),
/* Values are hard-coded for this example, it's usually best to bring these in via file or environment variable for production */
client = redis.createClient({
port : 6379, // replace with your port
host : '120.0.0.1', // replace with your hostanme or IP address
password : 'your password', // replace with your password
// optional, if using SSL
// use `fs.readFile[Sync]` or another method to bring these values in
tls : {
key : stringValueOfKeyFile,
cert : stringValueOfCertFile,
ca : [ stringValueOfCaCertFile ]
}
});
@stockholmux
Copy link
Author

OK. This is pretty straight forward (if I understand your intended use). You could create a class to do this but honestly, I don't know what you'd gain. Here are the things you need to know:

  • node_redis will manage the connection for you - you don't need to wait for it to connect to start sending redis commands, everything will just queue up.
  • Upon getting data the id, you would execute a client.get to your requested key. In Express, you can do this either in callback directly (app.post('/whatever/', (req,res) => { client.get( .... ) })) or implement this as a middleware which would be cleaner.
  • 500 requests per second should be absolutely no problem (assuming they are not gigantic). A single Redis instance should be good for tens of thousand ops/sec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment