Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable OPCache temporarily

Tags:

php

opcache

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

How can I temporarily disable the cache?

I tried :

 ini_set('opcache.enable', 0); 

But it has no effect.

Thanks

like image 846
SoCkEt7 Avatar asked Feb 04 '14 15:02

SoCkEt7


People also ask

Is Opcache enabled by default?

Using OPcache with WordPressOPcache is enabled by default which you can see in a phpinfo.

How do I turn on Opcache?

In the SOFTWARE section of the cPanel home screen, click Select PHP Version. Select the check box next to the opcode caching extension you want to enable: If you are using PHP version 5.4 or older, select apc. If you are using PHP version 5.5 or newer, select opcache.

How can I tell if Opcache is running?

To check if the caching engine works properly, just look at the percentages at the “Overview” tab at the opcache-gui page. If the memory usage and hit rate values are greater than zero, it means that the OpCache is caching the PHP code and the cached files are being used to handle the requests.

What does Opcache stand for?

What is OPcache or PHP Opcode Caching? OPcache is a type of OPcode caching. This kind of caching compiles human-readable PHP code to code your server understands which is called opcode. This occurs when the PHP file loads on a web page for the first time.


2 Answers

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

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

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

    opcache.enable=0 

In all cases, you can also use good old system-wide php.ini if you have access to it.

like image 155
Álvaro González Avatar answered Sep 22 '22 11:09

Álvaro González


opcache.enable is PHP_INI_ALL which means that ini_set() does work, 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_file script.

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.

The .htaccess php_flag directives only apply if you are running mod_php or equivalent. They and .user.ini files are PHP_INI_PERDIR, which will also be latched at request activation.

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=0 in the appropriate INI file and restart your webserver. Set it back to =1 and restart again when you are done.

Also consider (in the dev context) setting opcache.validate_timestamps=on and 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.

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

like image 36
TerryE Avatar answered Sep 23 '22 11:09

TerryE