警告:opcache_invalidate():在 /var/www/app/src/Cache/OpcacheManager.php:15 中没有该文件或目录
Warning: opcache_invalidate(): No such file or directory in /var/www/app/src/Cache/OpcacheManager.php:15
ID: php/opcache-invalidate-failure
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| php:8.1.0 | active | — | — | — |
| php:8.2.0 | active | — | — | — |
| php:8.3.0 | active | — | — | — |
根因分析
opcache_invalidate() 函数被调用时使用了文件系统中不存在的文件路径,通常是由于缓存键过期,或在构建缓存与尝试失效之间文件被删除。
English
The opcache_invalidate() function is called with a file path that does not exist on the filesystem, often due to a stale cache key or a file that was deleted between the time the cache was built and the invalidation attempt.
官方文档
https://www.php.net/manual/en/function.opcache-invalidate.php解决方案
-
在调用 opcache_invalidate() 之前,使用 file_exists() 检查文件是否存在:if (file_exists($filePath)) { opcache_invalidate($filePath); } else { // 记录日志或优雅处理 } -
使用 opcache_get_status() 列出缓存的文件,确保在尝试失效之前文件已被缓存:$status = opcache_get_status(false); if (isset($status['scripts'][$filePath])) { opcache_invalidate($filePath); } -
如果文件路径是动态的,使用 realpath() 规范化路径以解析符号链接和相对路径:$realPath = realpath($filePath); if ($realPath !== false) { opcache_invalidate($realPath); }
无效尝试
常见但无效的做法:
-
90% 失败
Ignoring the warning and continuing to call opcache_invalidate() with non-existent paths leads to OPcache never being properly invalidated, which can cause stale code to be served indefinitely.
-
80% 失败
Increasing OPcache memory limits (opcache.memory_consumption) does not address the root cause of invalid file paths, as the warning is about file existence, not memory.
-
70% 失败
Disabling OPcache entirely (opcache.enable=0) eliminates the warning but also removes all caching benefits, significantly degrading application performance.