PHP缓存包含文件

标签 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 "step 2:\n";
    include 'foo.php';
  break;
}
?>

foo.php 最初有以下内容:

<?php print "foo\n"; ?>

当我在浏览器中调用 test.php?step=1 时,我希望得到以下输出:

step 2:
bar

但是我得到了这个输出:

step 2:
foo

当我注释掉第 5 行中的 include 时,我得到了想要的结果。结论是,PHP 缓存了 foo.php 的内容。当我使用 step=2 重新加载页面时,我也得到了想要的结果。

现在...这是为什么以及如何避免这种情况?

最佳答案

假设您使用 OPcache,opcache.enable = 0 有效。

一个更有效的方法是使用

opcache_invalidate ( string $script [, boolean $force = FALSE ] )

这将从内存中删除脚本的缓存版本并强制 PHP 重新编译。

关于PHP缓存包含文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27786825/

相关文章:

caching - HTML5 应用程序缓存与浏览器缓存

php - 想要在ZF2中使用一种形式插入两个表

php - 建立数据库连接时出错 - WP 主机更改了密码

caching - 使用 Azure AppFabric 缓存服务的最佳实践?

algorithm - 这种缓存/数据结构的名称是什么?

iOS网络问题。 TLS Session缓存漏洞

ios - 关闭应用程序后预取的 SDWebImage 不会保留

php - 我需要用 PHP 来输入所有内容吗?

php - 如何比较数组中的字符串匹配?

php - 如何使用 mysql_num_rows 加速这个查询?