简体   繁体   English

为node.js应用初始化redis数据库的推荐机制是什么?

[英]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. 我是node.js的新手,并且还不熟悉它周围的生态系统。

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. 我有一个一页的Express应用程序,该应用程序在redis中缓存了一些数据,我想在redis数据库中添加一些配置设置。 Thus far I have been simply using redis-cli to manually set the necessary keys. 到目前为止,我一直只是使用redis-cli手动设置必要的键。 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. 我宁愿以某种方式让npm运行脚本(或其他方式),以便在服务器上部署应用程序尽可能简单。

What is the recommended mechanism for initializing a redis database for a node.js app? node.js应用初始化redis数据库的推荐机制是什么?

Using the redis module on npm you could easily use mset() to set multiple keys in one go. 使用npm上的redis模块,您可以轻松地使用mset()一次性设置多个键。 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();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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