Warning: opcache_invalidate(): No such file or directory in /var/www/app/src/Cache/OpcacheManager.php:15
ID: php/opcache-invalidate-failure
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| php:8.1.0 | active | — | — | — |
| php:8.2.0 | active | — | — | — |
| php:8.3.0 | active | — | — | — |
Root Cause
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.
generic中文
opcache_invalidate() 函数被调用时使用了文件系统中不存在的文件路径,通常是由于缓存键过期,或在构建缓存与尝试失效之间文件被删除。
Official Documentation
https://www.php.net/manual/en/function.opcache-invalidate.phpWorkarounds
-
90% success Before calling opcache_invalidate(), check if the file exists using file_exists(): if (file_exists($filePath)) { opcache_invalidate($filePath); } else { // log or handle gracefully }
Before calling opcache_invalidate(), check if the file exists using file_exists(): if (file_exists($filePath)) { opcache_invalidate($filePath); } else { // log or handle gracefully } -
85% success Use opcache_get_status() to list cached files and ensure the file is actually cached before attempting invalidation: $status = opcache_get_status(false); if (isset($status['scripts'][$filePath])) { opcache_invalidate($filePath); }
Use opcache_get_status() to list cached files and ensure the file is actually cached before attempting invalidation: $status = opcache_get_status(false); if (isset($status['scripts'][$filePath])) { opcache_invalidate($filePath); } -
80% success If the file path is dynamic, normalize it with realpath() to resolve symlinks and relative paths before invalidation: $realPath = realpath($filePath); if ($realPath !== false) { opcache_invalidate($realPath); }
If the file path is dynamic, normalize it with realpath() to resolve symlinks and relative paths before invalidation: $realPath = realpath($filePath); if ($realPath !== false) { opcache_invalidate($realPath); }
中文步骤
在调用 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); }
Dead Ends
Common approaches that don't work:
-
90% fail
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% fail
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% fail
Disabling OPcache entirely (opcache.enable=0) eliminates the warning but also removes all caching benefits, significantly degrading application performance.