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 ]
}
});
@shubhdip86
Copy link

HI,

Would like to request you, please help any sample code to user redis node connection using singleton class and able to store redis table data in variable.

@stockholmux
Copy link
Author

@shubhdip86 could you tell me a little more about what you're trying to do? It sounds like you're trying to create a new connection every time you need some data - I don't think that's a very good idea.

@shubhdip86
Copy link

Hello,

Thank you.

I have developed an application in NodeJS express POST data receiver. where every client post some data with their signature id. As per their signature id I need to response different pass-code. The pass code are stored in redis server in key pair value. I need to create a single class interface which can be import and use fetch data from redis or put data in redis server using redis-node. My get/put request will be 500 per second.
Please suggest the better approch

@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