PHP:clearstatcache() 不起作用

发布于 2024-12-19 14:51:59 字数 999 浏览 2 评论 0原文

我的工作流程非常简单:

  1. 我删除一个
  2. 名为 clearstatcache()
  3. 文件我保存一个
  4. 名为 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:

  1. I delete a file
  2. I call clearstatcache()
  3. I save a new file
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肩上的翅膀 2024-12-26 14:51:59

我同意@hakre,检查创建/删除的结果。

这不是快速而肮脏的,甚至 Rasmus 本人推荐它

[2011-03-31 08:34 UTC] [电子邮件受保护]

你们知道统计缓存是按请求计算的,对吗?只有你
需要在 file_exists() 调用之前清除统计缓存,如果您 A.
对其进行统计,然后 B. 在此基础上创建或删除它
要求。在这种情况下,您不需要再次统计它,因为
创建/删除的成功状态将告诉您该文件是否是
有或没有。也许对于长时间运行的守护进程或其他东西
变得更成问题,但对于典型的 Web 请求,统计缓存
通常可以为您节省数十次系统调用。

如果你真的坚持使用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:

[2011-03-31 08:34 UTC] [email protected]

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文