php runtime_error ai_generated true

警告: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

其他格式: JSON · Markdown 中文 · English
75%修复率
82%置信度
1证据数
2024-01-10首次发现

版本兼容性

版本状态引入弃用备注
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.

generic

官方文档

https://www.php.net/manual/en/function.opcache-invalidate.php

解决方案

  1. 在调用 opcache_invalidate() 之前,使用 file_exists() 检查文件是否存在:if (file_exists($filePath)) { opcache_invalidate($filePath); } else { // 记录日志或优雅处理 }
  2. 使用 opcache_get_status() 列出缓存的文件,确保在尝试失效之前文件已被缓存:$status = opcache_get_status(false); if (isset($status['scripts'][$filePath])) { opcache_invalidate($filePath); }
  3. 如果文件路径是动态的,使用 realpath() 规范化路径以解析符号链接和相对路径:$realPath = realpath($filePath); if ($realPath !== false) { opcache_invalidate($realPath); }

无效尝试

常见但无效的做法:

  1. 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.

  2. 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.

  3. 70% 失败

    Disabling OPcache entirely (opcache.enable=0) eliminates the warning but also removes all caching benefits, significantly degrading application performance.