Node.js – Redis 中的 client.end 方法
该方法强行关闭所有与Redis服务器的连接,无需等到所有回复都解析完毕。此方法只是关闭节点和Redis服务器之间的所有连接和正在进行的流。如果你想干净地退出,你应该使用方法。client.end(flush)client.quit()
语法
client.end(flush)
参数
flush-此输入参数将包含一个布尔值,用于指示是否关闭连接。
示例1
创建一个名为“clientEnd.js”的文件并复制以下代码。创建文件后,使用命令“nodeclientEnd.js”运行此代码,如下例所示-
//client.end()演示示例 //导入redis模块 const redis = require("redis"); //创建redis客户端 const client = redis.createClient(); client.set("Hello", "nhooo", function(err) { //这将返回错误因为client.endis设置为true console.error(err); }); //不会处理进一步的命令 client.end(true); client.get("hello", function(err) { console.error(err); //连接已经关闭。 });输出结果
它将产生以下输出-
C:\home\node>> node clientEnd.js { AbortError: Connection forcefully ended and command aborted. at RedisClient.flush_and_error (/home/nhooo/example/node_modules/redis/index.js:298:23) at RedisClient.end (/home/nhooo/example/node_modules/redis/lib/extendedApi. js:52:14) at Object.<anonymous> (/home/nhooo/example/redis.js:19:8) atModule._compile(internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) atModule.load(internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) at startup (internal/bootstrap/node.js:283:19) code: 'NR_CLOSED', command: 'SET', args: [ 'Hello', 'nhooo' ] } { AbortError: Connection forcefully ended and command aborted. at RedisClient.flush_and_error (/home/nhooo/example/node_modules/redis/index.js:298:23) at RedisClient.end (/home/nhooo/example/node_modules/redis/lib/extendedApi. js:52:14) at Object.<anonymous> (/home/nhooo/example/redis.js:19:8) atModule._compile(internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) atModule.load(internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Function.Module.runMain (internal/modules/cjs/loader.js:831:12) at startup (internal/bootstrap/node.js:283:19) code: 'NR_CLOSED', command: 'GET', args: [ 'hello' ] }