nodejs 中的单例模式 - 是否需要? [英] Singleton pattern in nodejs - is it needed?

查看:15
本文介绍了nodejs 中的单例模式 - 是否需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了这篇文章如何在 Node.js 中编写单例.我知道 require 的文档声明:

I recently came across this article on how to write a singleton in Node.js. I know the documentation of require states that:

模块在第一次加载后被缓存.多次调用 require('foo') 可能不会导致模块代码被多次执行.

Modules are cached after the first time they are loaded. Multiple calls to require('foo') may not cause the module code to be executed multiple times.

因此,似乎每个需要的模块都可以轻松地用作单例,而无需单例样板代码.

So it seems that every required module can be easily used as a singleton without the singleton boilerplate-code.

问题:

以上文章是否提供了创建单例的全面解决方案?

Does the above article provide a round about solution to creating a singleton?

推荐答案

这基本上与 nodejs 缓存有关.简单明了.

https://nodejs.org/api/modules.html#modules_caching

(v 6.3.1)

模块在第一次加载后被缓存.这意味着(除其他外)每次调用 require('foo') 都会得到返回完全相同的对象,如果它会解析为相同的文件.

Caching

Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.

多次调用 require('foo') 可能不会导致模块代码被执行多次.这是一个重要的特征.用它,可以返回部分完成"的对象,从而允许传递即使会导致循环,也要加载依赖项.

Multiple calls to require('foo') may not cause the module code to be executed multiple times. This is an important feature. With it, "partially done" objects can be returned, thus allowing transitive dependencies to be loaded even when they would cause cycles.

如果你想让一个模块多次执行代码,那么导出一个函数,然后调用该函数.

If you want to have a module execute code multiple times, then export a function, and call that function.

模块根据解析的文件名进行缓存.由于模块可能根据调用位置解析为不同的文件名模块(从 node_modules 文件夹加载),不能保证require('foo') 将总是返回完全相同的对象,如果它会解析为不同的文件.

Modules are cached based on their resolved filename. Since modules may resolve to a different filename based on the location of the calling module (loading from node_modules folders), it is not a guarantee that require('foo') will always return the exact same object, if it would resolve to different files.

此外,在不区分大小写的文件系统或操作系统上,不同的解析文件名可以指向同一个文件,但缓存仍将它们视为不同的模块并将重新加载文件多次.例如 require('./foo') 和 require('./FOO')返回两个不同的对象,无论是否 ./foo 和./FOO 是同一个文件.

Additionally, on case-insensitive file systems or operating systems, different resolved filenames can point to the same file, but the cache will still treat them as different modules and will reload the file multiple times. For example, require('./foo') and require('./FOO') return two different objects, irrespective of whether or not ./foo and ./FOO are the same file.

简单来说.

如果你想要一个单身人士;导出对象.

If you want a Singleton; export an object.

如果你不想要单例;导出函数(并在该函数中执行操作/返回操作/任何操作).

If you do not want a Singleton; export a function (and do stuff/return stuff/whatever in that function).

要非常清楚,如果您正确执行此操作,它应该可以工作,请查看 https://stackoverflow.com/a/33746703/1137669(艾伦·卢斯的回答).它在代码中解释了由于不同解析的文件名导致缓存失败时会发生什么.但是如果你总是解析为相同的文件名,它应该可以工作.

To be VERY clear, if you do this properly it should work, look at https://stackoverflow.com/a/33746703/1137669 (Allen Luce's answer). It explains in code what happens when caching fails due to differently resolved filenames. But if you ALWAYS resolve to the same filename it should work.

创造node.js 中真正的单例,带有 es6 符号另一种解决方案:在此链接中

这个答案指的是 CommonJS(Node.js 自己的导入/导出模块的方式).Node.js 很可能会切换到 ECMAScript 模块:https://nodejs.org/api/esm.html (如果你不知道,ECMAScript 就是 JavaScript 的真名)

This answer refers to CommonJS (Node.js's own way to import/export modules). Node.js will most likely be switching over to ECMAScript Modules: https://nodejs.org/api/esm.html (ECMAScript is the real name of JavaScript if you didn't know)

迁移到 ECMAScript 时,请暂时阅读以下内容:https://nodejs.org/api/esm.html#esm_writing_dual_packages_while_avoiding_or_minimizing_hazards

When migrating to ECMAScript read the following for now: https://nodejs.org/api/esm.html#esm_writing_dual_packages_while_avoiding_or_minimizing_hazards

这篇关于nodejs 中的单例模式 - 是否需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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