Node.js + Express + Redis,何时关闭连接? [英] Node.js + Express + Redis, when to close connection?

查看:311
本文介绍了Node.js + Express + Redis,何时关闭连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Node应用程序,该应用程序使用 Express 学习节点书中概述的方法,并为应用程序的寿命.使用这种方法,何时可以在redis客户端上调用close()?我什至需要吗?

I have a Node application that uses Express and node_redis. I'm following the approach outlined in the Learning Node book and creating a single client for the life of the application. Given this approach, when do I call close() on the redis client? Do I even need to?

相关代码

var express = require( 'express' ),
    redis = require( 'redis' );

var app = express(),
    config = require( './config/application' )[ app.get( 'env' ) ];

// create Redis client
var redisClient = redis.createClient();
redisClient.on( 'error', function( err ) {
  console.log( 'Error' + err );
} );
// select the database
redisClient.select( config.redis_database );

...
/* more setup, route defintions, etc. */
...

http.createServer( app ).listen( 4000, function() {
  console.log( 'Server started and ready for action!' );
});

推荐答案

您有两种选择.

  1. 保持懒惰,并为Redis服务器上的所有客户端设置空闲超时.然后,当客户端闲置时间过长时,服务器将终止它们的连接.
  2. 在节点进程退出时终止连接.

.

process.on("exit", function(){
    redisClient.quit();
});

这篇关于Node.js + Express + Redis,何时关闭连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆