编译缓存的可移植性
🌐 Portability of the compile cache
默认情况下,当被缓存模块的绝对路径发生变化时,缓存会失效。为了在移动项目目录后仍然保持缓存有效,可以启用可移植编译缓存。这样,只要相对于缓存目录的布局保持不变,以前编译的模块就可以在不同的目录位置重复使用。这是基于最佳努力的方式进行的。如果 Node.js 无法计算模块相对于缓存目录的位置,该模块将不会被缓存。
🌐 By default, caches are invalidated when the absolute paths of the modules being cached are changed. To keep the cache working after moving the project directory, enable portable compile cache. This allows previously compiled modules to be reused across different directory locations as long as the layout relative to the cache directory remains the same. This would be done on a best-effort basis. If Node.js cannot compute the location of a module relative to the cache directory, the module will not be cached.
便携缓存也不会按用户拆分:在有 UID 的平台上,非便携缓存的缓存子目录会以创建它的用户的 UID 作为后缀,所以只有该用户能找到它,而便携缓存对每个用户使用相同的子目录。这让一次生成的缓存(例如在构建时,然后作为只读随应用一起分发)可以被任何运行代码的人读取;无法写入目录的用户仍然可以读取,写入失败只意味着模块会被重新编译。
🌐 A portable cache is also not split by user: on platforms with uids the cache subdirectory of a non-portable cache is suffixed with the uid of the user who created it, so it is only found by that user, while a portable cache uses the same subdirectory for every user. This lets a cache generated once (for example at build time, then shipped read-only with an application) be read by whoever runs the code; a user who cannot write to the directory still reads it, and a failed write only means the module is compiled again.
有两种方法可以启用便携模式:
🌐 There are two ways to enable the portable mode:
-
在
module.enableCompileCache()中使用便携选项:// Non-portable cache (default): cache breaks if project is moved module.enableCompileCache({ directory: '/path/to/cache/storage/dir' }); // Portable cache: cache works after the project is moved module.enableCompileCache({ directory: '/path/to/cache/storage/dir', portable: true });