PHP:clearstatcache() 不起作用
我的工作流程非常简单:
- 我删除一个
- 名为
clearstatcache()
的 - 文件我保存一个
- 名为
clearstatcache()
的
新文件不过,最后, is_file( )
返回 false
一段时间,然后决定在 10 秒后刷新时返回 true
。
看起来像是缓存问题,不是吗?
这是我的一段代码:
// step 1
$path = 'file_to_delete.jpg';
unlink($path);
// is_file($path) returns false here -- normal behavior
// step 2
clearstatcache();
// step 3 -- some stuff going on on an uploaded image, that leads to:
imagejpeg($imagetosave, $path, 80);
// step 4
clearstatcache();
// is_file() returns false, i have to wait a couple of seconds before it starts returning true
谢谢您的帮助!
编辑:
鉴于我得到的所有答案,问题似乎并非来自clearstatcache()
。
但我应该补充一点,当我覆盖文件时(因此其现有状态不会改变),is_file()
返回良好的结果。但当其现有状态实际发生变化时,问题就会发生。如果错误不是来自 clearstatcache()
,那会很奇怪,对吧? (或者确实与此缓存相关的东西)
My workflow is pretty simple:
- I delete a file
- I call
clearstatcache()
- I save a new file
- I call
clearstatcache()
Still, at the end, is_file()
returns false
for a while before deciding to return true
when i refresh 10sec later for example.
It looks like a cache problem, doesn't it?
Here's a piece of my code:
// step 1
$path = 'file_to_delete.jpg';
unlink($path);
// is_file($path) returns false here -- normal behavior
// step 2
clearstatcache();
// step 3 -- some stuff going on on an uploaded image, that leads to:
imagejpeg($imagetosave, $path, 80);
// step 4
clearstatcache();
// is_file() returns false, i have to wait a couple of seconds before it starts returning true
Thank you for your help!
EDIT:
Given all the answers i had, the problem doesn't seem to come from clearstatcache()
.
But should i add, when i overwrite the file (thus its existing status doesn't change), is_file()
returns the good result. but when its existing status actually changes, the problem happens. It would be weird if the error didn't come from clearstatcache()
, right? (or something related to this cache indeed)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我同意@hakre,检查创建/删除的结果。
这不是快速而肮脏的,甚至 Rasmus 本人推荐它:
如果你真的坚持使用
clearstatcache()
,我想到的唯一一件事(除了写入缓存)是,你可能有一个非常巨大的上传文件夹,其中包含数千个文件。在一个文件夹中包含很多很多文件肯定会减慢重新声明的速度。
如果是这样,请尝试减少文件数量以获得统计性能,例如通过创建多个文件夹,为上传文件名的每个初始字符创建一个文件夹,例如
upload/a/
、upload/ b/
等I agree with @hakre, check the result of the create/delete.
It's not quick-and-dirty, even Rasmus himself recommends it:
If you really insist on using
clearstatcache()
, the only thing (other than write caching) coming to my mind is, that you mave have a very huge upload folder, containing thousands of files.Having many, many files in a single folder will definitely slow down re-stating.
If so, try to reduce the number of files to gain stat performance, e.g. by creating multiple folders, one folder for each initial char of your upload filenames, like
upload/a/
,upload/b/
, etc.