PHP缓存包含文件 [英] PHP caching include file
问题描述
我在test.php中有以下测试代码:
I have the following test code in test.php:
<?php
$step = $_GET['step'];
switch($step) {
case 1:
include 'foo.php'; # line 5
file_put_contents('foo.php', '<?php print "bar\\n"; ?>');
header('Location: test.php?step=2');
break;
case 2:
print "step 2:\n";
include 'foo.php';
break;
}
?>
foo.php最初具有以下内容:
foo.php initially has the following content:
<?php print "foo\n"; ?>
当我在浏览器中调用test.php?step = 1时,我会期望得到以下输出:
When I call test.php?step=1 in my browser I would expect the following output:
step 2:
bar
但我得到以下输出:
step 2:
foo
当我注释掉第5行中的include时,我得到了期望的结果.结论是,PHP缓存了foo.php的内容.当我用step = 2重新加载页面时,我也得到了预期的结果.
When I comment out the include in line 5, I get the desired result. The conclusion is, that PHP caches the content of foo.php. When I reload the page with step=2 I also get the desired result.
现在...这是为什么,以及如何避免这种情况?
Now... why is this and how to avoid this?
推荐答案
假设您使用OPcache, opcache.enable = 0
可以工作.
Assuming you use OPcache, opcache.enable = 0
works.
一种更有效的方法是使用
A more efficient method is to use
opcache_invalidate ( string $script [, boolean $force = FALSE ] )
这将从内存中删除脚本的缓存版本,并强制PHP重新编译.
This will remove the cached version of the script from memory and force PHP to recompile.
这篇关于PHP缓存包含文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!