clearstatcache() is really slow (> 2ms), at least on Apache mod_php 5.2.3/WinXP on Athlon 64 X2 5600+.
I'm benchmarking a script with 2000 hits, 100 simultaneous.
Without clearstatcache(): 430 req/sec (2.33 ms mean)
With clearstatcache(): 214 req/sec (4.66 ms mean)
clearstatcache
(PHP 4, PHP 5)
clearstatcache — 清除文件状态缓存
说明
void clearstatcache
( void
)
当使用 stat(),lstat() 或者任何列在受影响函数表(见下面)中的函数时,PHP 将缓存这些函数的返回信息以提供更快的性能。然而在某些情况下,你可能想清除被缓存的信息。例如如果在一个脚本中多次检查同一个文件,而该文件在此脚本执行期间有被删除或修改的危险时,你需要清除文件状态缓存。这种情况下,可以用 clearstatcache() 函数来清除被 PHP 缓存的该文件信息。
必须注意的是,对于不存在的文件,PHP 并不会缓存其信息。所以如果调用 file_exists() 来检查不存在的文件,在该文件没有被创建之前,它都会返回 FALSE。如果该文件被创建了,就算以后被删除,它都会返回 TRUE
Note: 本函数缓存特定文件名的信息,因此只在对同一个文件名进行多次操作并且需要该文件信息不被缓存时才需要调用 clearstatcache()。
受影响的函数包括 stat(),lstat(),file_exists(),is_writable(),is_readable(),is_executable(),is_file(),is_dir(),is_link(),filectime(),fileatime(),filemtime(),fileinode(),filegroup(),fileowner(),filesize(),filetype() 和 fileperms()。
clearstatcache
steve at mrclay.org
08-Jun-2008 06:28
08-Jun-2008 06:28
stangelanda at gmail dot com
25-Jan-2008 04:35
25-Jan-2008 04:35
It should be noted that a call to any of those functions will cache all of the file's information, not just the information that is returned.
Obviously it is clear that the following requires you to clear the cache.
<?php
$size1 = filesize($filename);
unlink($filename);
$size2 = filesize($filename);
// $size2 still equals $size1, unless you cleared the stat cache in between.
?>
<?php
$access = fileatime($filename);
unlink($filename);
$size = filesize($filename);
// $size will be the filesize before it was unlinked, because the filesize was cached when fileatime was called, even though the two functions wouldn't appear to have anything to do with either other.
?>
Confirmed on a windows system.
