module.enableCompileCache([cacheDir])


稳定性: 1.1 - 积极开发

¥Stability: 1.1 - Active Development

  • cacheDir <string> | <undefined> 可选路径,用于指定将存储/检索编译缓存的目录。

    ¥cacheDir <string> | <undefined> Optional path to specify the directory where the compile cache will be stored/retrieved.

  • 返回:<Object>

    ¥Returns: <Object>

    • status <integer> module.constants.compileCacheStatus 之一

      ¥status <integer> One of the module.constants.compileCacheStatus

    • message <string> | <undefined> 如果 Node.js 无法启用编译缓存,则包含错误消息。仅当 statusmodule.constants.compileCacheStatus.FAILED 时才设置。

      ¥message <string> | <undefined> If Node.js cannot enable the compile cache, this contains the error message. Only set if status is module.constants.compileCacheStatus.FAILED.

    • directory <string> | <undefined> 如果启用了编译缓存,则包含存储编译缓存的目录。仅当 statusmodule.constants.compileCacheStatus.ENABLEDmodule.constants.compileCacheStatus.ALREADY_ENABLED 时才设置。

      ¥directory <string> | <undefined> If the compile cache is enabled, this contains the directory where the compile cache is stored. Only set if status is module.constants.compileCacheStatus.ENABLED or module.constants.compileCacheStatus.ALREADY_ENABLED.

在当前 Node.js 实例中启用 模块编译缓存

¥Enable module compile cache in the current Node.js instance.

如果未指定 cacheDir,Node.js 将使用 NODE_COMPILE_CACHE=dir 环境变量指定的目录(如果已设置)或使用 path.join(os.tmpdir(), 'node-compile-cache')。对于一般用例,建议在不指定 cacheDir 的情况下调用 module.enableCompileCache(),以便目录可以在必要时被 NODE_COMPILE_CACHE 环境变量覆盖。

¥If cacheDir is not specified, Node.js will either use the directory specified by the NODE_COMPILE_CACHE=dir environment variable if it's set, or use path.join(os.tmpdir(), 'node-compile-cache') otherwise. For general use cases, it's recommended to call module.enableCompileCache() without specifying the cacheDir, so that the directory can be overridden by the NODE_COMPILE_CACHE environment variable when necessary.

由于编译缓存应该是一种安静的优化,应用不需要它才能运行,因此此方法被设计为在无法启用编译缓存时不会抛出任何异常。相反,它将返回一个包含 message 字段中的错误消息的对象以帮助调试。如果成功启用编译缓存,则返回对象中的 directory 字段包含存储编译缓存的目录的路径。返回对象中的 status 字段将是 module.constants.compileCacheStatus 值之一,用于指示尝试启用 模块编译缓存 的结果。

¥Since compile cache is supposed to be a quiet optimization that is not required for the application to be functional, this method is designed to not throw any exception when the compile cache cannot be enabled. Instead, it will return an object containing an error message in the message field to aid debugging. If compile cache is enabled successfully, the directory field in the returned object contains the path to the directory where the compile cache is stored. The status field in the returned object would be one of the module.constants.compileCacheStatus values to indicate the result of the attempt to enable the module compile cache.

此方法仅影响当前 Node.js 实例。要在子工作线程中启用它,请在子工作线程中也调用此方法,或将 process.env.NODE_COMPILE_CACHE 值设置为编译缓存目录,以便可以将行为继承到子工作线程中。可以从此方法返回的 directory 字段或使用 module.getCompileCacheDir() 获取目录。

¥This method only affects the current Node.js instance. To enable it in child worker threads, either call this method in child worker threads too, or set the process.env.NODE_COMPILE_CACHE value to compile cache directory so the behavior can be inherited into the child workers. The directory can be obtained either from the directory field returned by this method, or with module.getCompileCacheDir().