简体   繁体   English

Node.js 本机插件的不同实例之间的全局共享数据?

[英]Global shared data between different instances of a Node.js native addon?

I'm looking to create a native addon with Node-API (more specifically with the C++ wrapper module node-addon-api ).我正在寻找使用 Node-API 创建本机插件(更具体地说是使用 C++ 包装器模块node-addon-api )。

I need to have global read-only data that will be loaded once and will be shared between different instances of the addon (eg when the addon is loaded by different node.js workers).我需要全局只读数据,这些数据将被加载一次,并将在插件的不同实例之间共享(例如,当插件由不同的 node.js 工作人员加载时)。

Is there a recommended way to do this?有没有推荐的方法来做到这一点? Would it be safe to just store the data in a static variable?将数据存储在静态变量中是否安全?

It depends on the type of your data.这取决于您的数据类型。 If your data does not involve any V8 data types (or Napi:: types), then, yes, it is generally safe.如果您的数据不涉及任何 V8 数据类型(或 Napi:: 类型),那么,是的,它通常是安全的。

Just be careful to not shared a variable that has been initialized with Node::Env - this is the rule to follow - if it needs Node::Env then it can't be shared.请注意不要共享已用Node::Env初始化的变量 - 这是要遵循的规则 - 如果它需要Node::Env则不能共享。

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

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