PHP缓存包含文件
我在test.php中有以下测试代码:PHP缓存包含文件,php,caching,Php,Caching,我在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
<?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;
}
?>
但我得到的结果是:
step 2:
foo
当我注释掉第5行中的include时,我得到了期望的结果。结论是,PHP缓存了foo.PHP的内容。当我用step=2重新加载页面时,我也会得到所需的结果
现在。。。为什么会出现这种情况以及如何避免这种情况?假设您使用OPcache,
OPcache.enable=0
可以工作
一个更有效的方法是使用
opcache_invalidate ( string $script [, boolean $force = FALSE ] )
这将从内存中删除脚本的缓存版本,并强制PHP重新编译。请注意,
opcache\u invalidate
并不总是可用的。因此,最好检查它是否存在。此外,您应该同时检查opcache\u invalidate
和apc\u compile\u file
以下函数将执行所有操作:
public static function clearCache($path){
if (function_exists('opcache_invalidate') && strlen(ini_get("opcache.restrict_api")) < 1) {
opcache_invalidate($path, true);
} elseif (function_exists('apc_compile_file')) {
apc_compile_file($path);
}
}
公共静态函数clearCache($path){
if(函数_存在('opcache_invalidate')&&strlen(ini_get(“opcache.restrict_api”)<1){
opcache_invalidate($path,true);
}elseif(函数_存在('apc_编译_文件')){
apc_编译_文件($path);
}
}
您使用的是什么版本的PHP?我猜您永远不会被重定向到?step=2
,因为您在发送位置
标题之前打印数据(这会导致错误)。如果不覆盖foo.PHP,则注释掉的第5行将无法使用。我还研究了foo.php。。。它被修改了。cOle2:如果它不被重定向,它将不会打印“步骤2:”!如果您仍然认为它正在缓存,那么应该检查是否启用了OpCache。php.ini中的opcache.enable=0。
opcache_invalidate ( string $script [, boolean $force = FALSE ] )
public static function clearCache($path){
if (function_exists('opcache_invalidate') && strlen(ini_get("opcache.restrict_api")) < 1) {
opcache_invalidate($path, true);
} elseif (function_exists('apc_compile_file')) {
apc_compile_file($path);
}
}