简体   繁体   中英

What is the recommended mechanism for initializing a redis database for a node.js app?

I am new to node.js , and am not yet familiar with the ecosystem around it.

I have a one-page Express app that caches some data in redis and I would like to add a few configuration settings to the redis db. Thus far I have been simply using redis-cli to manually set the necessary keys. I would prefer to somehow have npm run a script (or something), so that deploying the app on a server is as simple as possible.

What is the recommended mechanism for initializing a redis database for a node.js app?

Using the redis module on npm you could easily use mset() to set multiple keys in one go. Example:

var redis  = require('redis'),
    client = redis.createClient();

var kv = [
  'key1', 'value1',
  'key2', 2,
  'key3', 'value3'
];
client.mset(kv, function(err) {
  if (err) throw err;

  // end the connection gracefully if
  // you don't need to access redis anymore
  client.quit();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM