CakeFest 2024: The Official CakePHP Conference

clearstatcache

(PHP 4, PHP 5, PHP 7, PHP 8)

clearstatcacheDosya durum önbelleğini temizler

Açıklama

clearstatcache(bool $gerçek_yol_belleğini_temizle = false, string $dosyaismi = ""): void

Aşağıda verilen etkilenen işlevler listesindeki işlevlerden başka stat() veya lstat() işlevini kullanırsanız PHP işlemi hızlandırmak için bu işlevlerin döndürdüğü bilgiyi önbelleğe alır. Bununla birlikte belli durumlarda önbellekteki bilgiyi temizlemek ihtiyacını duyabilirsiniz. Örneğin hep aynı dosyanın varlığını sınayan bir betik için dosyanın işlem sırasında silinmesi veya değişmesi tehlikesi varsa durum önbelleğini temizlememeyi seçebilirsiniz. Bu gibi durumlarda, PHP'nin dosya hakkında önbelleğe aldığı bilgiyi temizlemek için clearstatcache() işlevini kullanabilirsiniz.

PHP'nin mevcut olmayan dosyalar hakkında önbellekte bilgi saklamadığına dikkat ediniz. Bu bakımdan, varolmayan bir dosya üzerinde file_exists() işlevini çağırırsanız, siz dosyayı oluşturana kadar işlev false döndürecektir. Siz dosyayı oluşturduktan sonra dosyayı silseniz bile işlev true döndürecektir. Bunun yanında, unlink() işlevi önbelleği otomatik olarak temizleyecektir.

Bilginize:

Bu işlev belli dosya isimleri hakkında bilgiyi önbelleğe alır; dolayısıyla aynı dosya üzerinde çok sayıda işlem yapıyorsanız ve bu dosya hakkındaki bilgilerin önbellekte saklanmaması gerekiyorsa tek ihtiyacınız clearstatcache() çağrısı yapmak olacaktır.

Önbelleklemeden etkilenen işlevler: stat(), lstat(), file_exists(), is_writable(), is_readable(), is_executable(), is_file(), is_dir(), is_link(), filectime(), fileatime(), filemtime(), fileinode(), filegroup(), fileowner(), filesize(), filetype() ve fileperms().

Bağımsız Değişkenler

gerçek_yol_belleğini_temizle

Gerçek yol öbbelleğinin de ayrıca temizleneceği bilgisi.

dosyaismi

Gerçek yol önbelleği sadece belirtilen dosya için ve sadece gerçek_yol_belleğini_temizle bağımsız değişkenine true atandığı takdirde temizlenir.

Dönen Değerler

Hiçbir değer dönmez.

Örnekler

Örnek 1 - clearstatcache() örneği

<?php
$file
= 'output_log.txt';

function
get_owner($file)
{
$stat = stat($file);
$user = posix_getpwuid($stat['uid']);
return
$user['name'];
}

$format = "UID @ %s: %s\n";

printf($format, date('r'), get_owner($file));

chown($file, 'ross');
printf($format, date('r'), get_owner($file));

clearstatcache();
printf($format, date('r'), get_owner($file));
?>

Yukarıdaki örnek şuna benzer bir çıktı üretir:

UID @ Sun, 12 Oct 2008 20:48:28 +0100: root
UID @ Sun, 12 Oct 2008 20:48:28 +0100: root
UID @ Sun, 12 Oct 2008 20:48:28 +0100: ross

add a note

User Contributed Notes 7 notes

up
10
vechenjivot at gmail dot com
3 years ago
Not documented, but seems like clearstatcache() is clearing the cache only for the process it is being called from. I have 2 PHP scripts running simultaneously, and the first one does call clearstatcache(), but still the second one deadlocks, unless I call clearstatcache() in it too:

script1:
<?php
touch
('system.lock');
...
unlink('system.lock');
clearstatcache(); // should be done by unlink?
?>

script2:
<?php
while (is_file('system.lock') {
sleep(1);
clearstatcache(); // without this, script 2 will deadlock forever!
}
?>

I also found this page, which leads to the same conclusion:
https://stackoverflow.com/questions/9251237/clearstatcache-include-path-sessions
up
10
msaladna at apisnetworks dot com
4 years ago
clearstatcache() does not canonicalize the path. clearstatcache(true, "/a/b/c") is different from clearstatcache(true, "/a/b//c").
up
35
matt_m at me dot com
12 years ago
unlink() does not clear the cache if you are performing file_exists() on a remote file like:

<?php
if (file_exists("ftp://ftp.example.com/somefile"))
?>

In this case, even after you unlink() successfully, you must call clearstatcache().

<?php
unlink
("ftp://ftp.example.com/somefile");
clearstatcache();
?>

file_exists() then properly returns false.
up
7
David Spector
5 years ago
Note that this function affects only file metadata. However, all the PHP file system functions do their own caching of actual file contents as well. You can use the "realpath_cache_size = 0" directive in PHP.ini to disable the content caching if you like. The default content caching timeout is 120 seconds.

Content caching is not a good idea during development work and for certain kinds of applications, since your code may read in old data from a file whose contents you have just changed.

Note: This is separate from the caching typically done by browsers for all GET requests (the majority of Web accesses) unless the HTTP headers override it. It is also separate from optional Apache server caching.
up
9
bj at wjblack dot com
9 years ago
Just to make this more obvious (and so search engines find this easier):

If you do fileops of any kind outside of PHP (say via a system() call), you probably want to clear the stat cache before doing any further tests on the file/dir/whatever. For example:

<?php
// is_dir() forces a stat call, so the cache is populated
if( is_dir($foo) ) {
system("rm -rf " . escapeshellarg($foo));
if(
is_dir($foo) ) {
// ...will still be true, even if the rm succeeded, because it's just
// reading from cache, not re-running the stat()
}
}
?>

Pop a clearstatcache() after the system call and all is good (modulo a bit of a performance hit from having a cleared stat cache :-( ).
up
1
Gabriel
5 years ago
Definition of $filename parameter let's you think that it expects the filename only but it works if you give the path + filename also.

It should be more clear about this.
up
-39
markandrewslade at gmail dot com
14 years ago
On Linux, a forked process inherits a copy of the parent's cache, but after forking the two caches do not impact each other. The snippet below demonstrates this by creating a child and confirming outdated (cached) information, then clearing the cache, and getting new information.

<?php

function report($directory, $prefix = '') { printf('%sDoes %s exist? PHP says "%s"'. PHP_EOL, $prefix, $directory, is_dir($directory) ? 'yes' : 'no'); }
$target = './delete-me-before-running-statcache';

if (
is_dir($target)) {
die(
"Delete $target before running.\n");
}

echo
"Creating $target.\n";
mkdir($target) || die("Unable to create $target.\n");
report($target); // is_dir($target) is now cached as true

echo "Unlinking $target.\n";
rmdir($target) || die("Unable to unlink $target.\n");

// This will say "yes", which is old (inaccurate) information.
report($target);

if ((
$pid = pcntl_fork()) === -1) { die("Failed to pcntl_fork.\n"); }
elseif (
$pid === 0) {
// child
report($target, '<<child>> ');
echo
"<<child>> Clearing stat cache.\n";
clearstatcache();
report($target, '<<child>> ');
} else {
// parent
sleep(2); // move this to the child block to reverse the test.
report($target, '<<<parent>> ');
clearstatcache();
report($target, '<<<parent>> ');
}

?>
To Top