Node.js – Redis 中的 client.end 方法
node.jsserver side programmingprogramming
client.end(flush) 方法强制关闭与 Redis 服务器的所有连接,而无需等待所有回复都解析完毕。此方法只会关闭 Node 和 Redis 服务器之间的所有连接以及正在进行的流式传输。如果您想干净地退出,请使用 client.quit() 方法。
语法
client.end(flush)
参数
flush - 此输入参数将保存一个布尔值,用于指示是否关闭连接。
示例 1
创建一个名为"clientEnd.js"的文件并复制以下代码。创建文件后,使用命令"node clientEnd.js"运行此代码,如下例所示 −
// client.end() 演示示例 // 导入 redis 模块 const redis = require("redis"); // 创建 redis 客户端 const client = redis.createClient(); client.set("Hello", "tutorialsPoint", function(err) { // 由于 client.end 设置为 true,因此将返回错误 console.error(err); }); // 不会处理其他命令 client.end(true); client.get("hello", function(err) { console.error(err); // The connection has already been closed. });
输出
它将产生以下输出 −
C:\home
ode>> node clientEnd.js { AbortError: Connection forcefully ended and command aborted. at RedisClient.flush_and_error (/home/tutorialsPoint/example/node_modules/redis/index.js:298:23) at RedisClient.end (/home/tutorialsPoint/example/node_modules/redis/lib/extendedApi. js:52:14) at Object.<anonymous> (/home/tutorialsPoint/example/redis.js:19:8) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.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', 'tutorialsPoint' ] } { AbortError: Connection forcefully ended and command aborted. at RedisClient.flush_and_error (/home/tutorialsPoint/example/node_modules/redis/index.js:298:23) at RedisClient.end (/home/tutorialsPoint/example/node_modules/redis/lib/extendedApi. js:52:14) at Object.<anonymous> (/home/tutorialsPoint/example/redis.js:19:8) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.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' ] }