【发布时间】:2016-08-25 21:32:40
【问题描述】:
根据file_exists()的php文档
这个函数的结果被缓存。请参阅 clearstatcache() 了解 更多细节。
但它没有提供关于 php 将在缓存中保存该信息多长时间的信息。我检查了clearstatcache() 的文档 但没有这样的信息。这次谷歌搜索也没有帮助。
我正在构建一个应用程序,其中调用 file_exists 以生成一些输出,并且由于任何用户可以随时删除正在检查的文件,因此我需要在生成输出之前确定它是否真的存在。
在 file_exists() 之前调用 clearcache() 可以解决问题,但出于好奇,我想知道 PHP 将缓存文件信息多长时间(默认时间)以及我可以通过哪个变量来更改缓存过期时间?
编辑1:实际上,删除文件后再次使用file_exists()检查返回false,但如果是这样,文档中写的cache是什么意思?
编辑 2:
<?
var_dump(file_exists('/home/user/filecheck.php'));
sleep(20);
// after running script, sleep for 20 seconds just to quickly delete this file manually before file_exists is called again.
// unlink is not used since as per documentation it'll clear php cache.
var_dump(file_exists('/home/user/filecheck.php'));
?>
脚本的响应是
boolean true
boolean false
这意味着即使文件存在,即使相同的执行,php也不会缓存file_exists信息,那么为什么在文档中写“这个函数的结果被缓存”?
【问题讨论】:
-
PHP 在请求期间缓存该信息
-
请求持续时间???
-
查看this bug report -
You guys realize that the stat cache is per-request, right? You only need to clear the stat cache before a file_exists() call if you A. did a stat for it, and B. either created or deleted it on that request. In which case you shouldn't need to stat it again since the success status of the create/delete will tell you whether the file is there or not. Perhaps for long-running daemons or something this becomes more of an issue, but for a typical web request the stat cache typically saves you dozens of system calls.中的评论 -
@MarkBaker 您指的是日期为 2004-06-15 15:43 的错误现在想起来并不奇怪。我认为这个问题在 11 年内得到了解决……
-
@num8er - 我特别指出了错误中的评论,该评论专门回答了 OP 关于缓存持续时间的问题;虽然我没有看到任何说明该错误已被关闭,但我希望它现在已经解决
标签: php