php 暂时禁用 OPCache

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21556437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:14:52  来源:igfitidea点击:

Disable OPCache temporarily

phpopcache

提问by Anthony

I recently moved to PHP 5.4 and installed OPCache, it's very powerful!

我最近迁移到 PHP 5.4 并安装了 OPCache,它非常强大!

How can I temporarily disable the cache?

如何暂时禁用缓存?

I tried :

我试过 :

 ini_set('opcache.enable', 0);

But it has no effect.

但它没有效果。

Thanks

谢谢

回答by álvaro González

Once your script runs, it's too late to not cache the file. You need to set it outside PHP:

一旦您的脚本运行,再不缓存文件就太晚了。您需要在 PHP 之外设置它:

  • If PHP runs as Apache module, use an .htaccessfile:

    php_flag opcache.enable Off
    
  • If PHP runs as CGI/FastCGI, use a .user.inifile:

    opcache.enable=0
    
  • 如果 PHP 作为 Apache 模块运行,请使用.htaccess文件:

    php_flag opcache.enable Off
    
  • 如果 PHP 以 CGI/FastCGI 运行,请使用.user.ini文件:

    opcache.enable=0
    

And, in any case, you can use good old system-wide php.iniif you have access to it.

而且,无论如何,php.ini如果您可以访问它,您可以在系统范围内使用良好的旧系统。

回答by TerryE

opcache.enableis PHP_INI_ALLwhich means that ini_set() doeswork, but only for current request to disable OPcache caching for the remainder of scripts compiled in your current request. (You can't force enabling). It reverts back to the system default for other requests. By this stage, the request script will already have been cached, unless you do the ini_set in an auto_prepend_filescript.

opcache.enablePHP_INI_ALL这意味着 ini_set()确实有效,但仅适用于当前请求,为当前请求中编译的其余脚本禁用 OPcache 缓存。(您不能强制启用)。对于其他请求,它会恢复为系统默认值。在这个阶段,请求脚本已经被缓存,除非你在auto_prepend_file脚本中执行 ini_set 。

The system defaults (PHP_INI_SYSTEM) are latched as part of PHP system startup and can't be reread. So in the case of Apache for example, you need to restart Apache to change / reload these.

系统默认值 ( PHP_INI_SYSTEM) 作为 PHP 系统启动的一部分被锁定并且无法重新读取。因此,以 Apache 为例,您需要重新启动 Apache 以更改/重新加载这些。

The .htaccessphp_flagdirectives only apply if you are running mod_php or equivalent. They and .user.inifiles are PHP_INI_PERDIR, which will also be latched at request activation.

这些 .htaccessphp_flag指令仅在您运行 mod_php 或等效程序时适用。它们和.user.ini文件是 PHP_INI_PERDIR,它也将在请求激活时锁定。

Now to the Q that I think that you might be asking. If you have a dev system then the easiest way is to set opcache.enable=0in the appropriate INI file and restart your webserver. Set it back to =1and restart again when you are done.

现在到我认为你可能会问的问题。如果您有一个开发系统,那么最简单的方法是opcache.enable=0在适当的 INI 文件中进行设置并重新启动您的网络服务器。完成后将其设置回=1并重新启动。

Also consider (in the dev context) setting opcache.validate_timestamps=onand opcache.revalidate_freq=0. This will keep OPcache enabled but scripts will be stat'ed on every compile request to see if they are changed. This gives the best of both worlds when developing.

还要考虑(在开发环境中)设置opcache.validate_timestamps=onopcache.revalidate_freq=0. 这将保持 OPcache 启用,但脚本将在每个编译请求上进行统计,以查看它们是否被更改。这在开发时提供了两全其美的优势。

Also read up on the opcache.blacklist_filenamedirective. This allow you to specify an exclusion file, so if this contains /var/www/test, and the web service docroot is /var/wwwthen anyscripts in the /var/www/test*hierarchies will not be cached.

另请阅读该opcache.blacklist_filename指令。这允许您指定一个排除文件,因此如果它包含/var/www/test,并且 web 服务 docroot 是/var/www那么层次结构中的任何脚本/var/www/test*都不会被缓存。

Hope this helps :)

希望这可以帮助 :)

回答by Jul

The best way i found in my case for disable opcache in a specific PHP file is : opcache_invalidate(__FILE__, true);

在我的案例中,我发现在特定 PHP 文件中禁用 opcache 的最佳方法是: opcache_invalidate(__FILE__, true);

You also can reset all cache with PHP : opcache_reset();

您还可以使用 PHP 重置所有缓存: opcache_reset();

回答by Nerque

In my modest opinion, because I'm no expert, Jul has given the best answer. The question included the term "temporarily", so changing the configuration files I think... is not the best answer because you need to reconfigure, run what you want and reconfigure again to make it work normally.

在我谦虚的意见中,因为我不是专家,Jul 给出了最好的答案。该问题包括“临时”一词,因此我认为更改配置文件......不是最佳答案,因为您需要重新配置,运行您想要的并再次重新配置以使其正常工作。

It's not smooth.

这并不顺利。

With the answer of Jul you can modify the code to perform some action by disabling the opcache and return to a normal situation within the same code (although we would have to see how to re-enable from code the opcache)

使用 Jul 的答案,您可以修改代码以通过禁用 opcache 来执行某些操作,并在同一代码中返回到正常情况(尽管我们必须了解如何从代码中重新启用 opcache)

For example, with Prestashop, there can be problems cleaning the "normal" cache from the administration interface if opcache is enabled, so in that case you can use a method so that when the action is performed, opcache is disabled, the "normal" cache is cleaned, and then opcache is enabled again.

例如,对于 Prestashop,如果启用了 opcache,从管理界面清除“普通”缓存可能会出现问题,因此在这种情况下,您可以使用一种方法,以便在执行操作时禁用 opcache,“普通”缓存缓存被清除,然后再次启用opcache。